/
FlickrMapViewController.m
237 lines (186 loc) · 7.12 KB
/
FlickrMapViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
//
// FlickrMapViewController.m
// MappingMashupApp
//
// Created by Ross Matsuda on 3/18/13.
// Copyright (c) 2013 The Peanut Gallery. All rights reserved.
//
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
#import "FlickrMapViewController.h"
#import "YelpMapViewController.h"
#import "YelpWebPageBrowser.h"
#import "AppDelegate.h"
#import "LocationManager.h"
#import "APIManager.h"
#import "Venue.h"
#import "Annotation.h"
#import "Photo.h"
#import "Business.h"
@interface FlickrMapViewController ()
{
LocationManager *locationManager;
Annotation *selectedAnnotation;
NSMutableArray *venuesArray;
__weak IBOutlet MKMapView *mapView;
}
@end
@implementation FlickrMapViewController
@synthesize managedObjectContext;
- (void)viewDidLoad
{
[super viewDidLoad];
// Core Data
AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];
self.managedObjectContext = appDelegate.managedObjectContext;
// Location Services
locationManager = appDelegate.locationManager;
[mapView setShowsUserLocation:YES];
APIManager *yelpAPIManager = [[APIManager alloc] initWithYelpSearch:@"free%20wifi" andLocation:locationManager];
yelpAPIManager.delegate = self;
// Allocate objects
// [possibly allocate the venuesArray later?]
venuesArray = [[NSMutableArray alloc]init];
venuesArray = [yelpAPIManager searchYelpParseResults];
[self addPinsToMap];
}
# pragma mark - User Location Methods
// deprecated: fix later
-(void)locationManager:(CLLocationManager*)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
//how many seconds ago was this new location created
NSTimeInterval time = [[newLocation timestamp] timeIntervalSinceNow];
//CLLocation manager will return last found location
//if this location was made more than 3 minutes ago, ignore
if (time<-180) {
return;
}
[self foundLocation:newLocation];
}
-(void)mapView:(MKMapView *)userMapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
CLLocationCoordinate2D loc = userLocation.coordinate;
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 250, 250);
[userMapView setRegion:region animated:YES];
}
# pragma mark - Annotation Methods
-(void)foundLocation:(CLLocation*)loc
{
CLLocationCoordinate2D coord = [loc coordinate];
//create an instances of annotation with the current data
Annotation *annotation = [[Annotation alloc]initWithCoordinate:coord
title:@"title"
subtitle:@"Somebody does not want poop"
yelpURL:@"http://www.catstache.biz"];
//add annotation to mapview
[mapView addAnnotation:annotation];
//zoom to region of location
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coord, 250, 250);
[mapView setRegion:region animated:YES];
[locationManager stopUpdatingLocation];
}
-(void)addPinsToMap
{
// make region our area
MKCoordinateSpan span =
{
.latitudeDelta = 0.01810686f,
.longitudeDelta = 0.01810686f
};
MKCoordinateRegion region = {locationManager.coordinate, span};
//set region to mapview
[mapView setRegion:region animated:YES];
for (int i = 0; i < venuesArray.count; i++)
{
CLLocation *venueLocation = [[venuesArray objectAtIndex:i] location];
NSString *venueName = [[venuesArray objectAtIndex:i] name];
//coordinate make
CLLocationCoordinate2D venueCoordinate;
venueCoordinate.longitude = venueLocation.coordinate.longitude;
venueCoordinate.latitude = venueLocation.coordinate.latitude;
//
// REVISE TO LEVERAGE NEW CUSTOM INITS //
//
// create annotation
// Annotation *myAnnotation = [[Annotation alloc] initWithPosition:placeCoordinate];
// myAnnotation.title = nameOfPlace;
// myAnnotation.subtitle = @"Demo subtitle";
//NSLog(@"%@", returnedArray);
//Add code here to capture yelp page URL
//NSString *yelpURLString = [[returnedArray objectAtIndex:i] valueForKey:@"yelpURL"];
//NSLog(@"%@", yelpURLString);
//myAnnotation.yelpPageURL = yelpURLString;
//add to map
//[myMapView addAnnotation:myAnnotation];
}
//NSLog(@"%@", [[myMapView.annotations objectAtIndex:0] title]);
}
-(MKAnnotationView*)mapView:(MKMapView*)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"myAnnotation"];
if (annotationView == nil) {
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
}
// [detailButton addTarget:self
// action:@selector(goToYelpPage)
// forControlEvents:UIControlEventTouchUpInside];
annotationView.canShowCallout = YES;
annotationView.image = [UIImage imageNamed:@"wifiIcon.png"];
annotationView.rightCalloutAccessoryView = detailButton;
if([annotation isKindOfClass: [MKUserLocation class]])
{
return nil;
}
return annotationView;
}
# pragma mark - User Actions
//
// User selects an annotation
//
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
selectedAnnotation = view.annotation;
//Note: This should break when we switch from Yelp annotations
//to Flickr photos.
//Once it doees work, delete this comment
Business *selectedBusiness = [NSEntityDescription insertNewObjectForEntityForName:@"Business" inManagedObjectContext:managedObjectContext];
selectedBusiness.name = view.annotation.title;
NSError *error;
if (![managedObjectContext save:&error])
{
NSLog(@"failed to save: %@", [error userInfo]);
}
//NSLog(@"Logging out the annotation %@", view.annotation.title);
}
//
// User taps on disclosure button to see more Yelp data.
//
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
NSLog(@"This is the method we want!");
// [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"m.yelp.com"]];
[self performSegueWithIdentifier:@"toYelpWebPage" sender:nil];
}
# pragma mark - Transitions
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
YelpMapViewController *ymvc = [segue destinationViewController];
// what's getting passed in
}
//
// Method for unwind action
//
-(IBAction) backToFlickrMapView: (UIStoryboardSegue *)segue
{
//Any additional actions to be performed during unwind
}
# pragma mark - End of document
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end