-
Notifications
You must be signed in to change notification settings - Fork 1
/
LocationSelectionViewController.m
449 lines (323 loc) · 12 KB
/
LocationSelectionViewController.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
//
// LocationSelectionViewController.m
// MilesTracker
//
// Created by Mike Glazer on 3/18/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "LocationSelectionViewController.h"
#import "CoreLocation/CLLocation.h"
#import "CoreLocation/CLLocationManager.h"
#import "UITableAlert.h"
#import "UITableAlertDataSource.h"
#import "EventLocationMapAnnotation.h"
@implementation LocationSelectionViewController
@synthesize mapView;
@synthesize delegate;
@synthesize locationManager;
@synthesize forwardGeocoder;
@synthesize addressSearchBar;
@synthesize existingAddress;
@synthesize existingCoordinate;
@synthesize currentAnnotation;
@synthesize selectedAddress;
@synthesize selectedCoordinate;
@synthesize updateLocationButton;
@synthesize toolbar;
/*
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
*/
// Implement loadView to create a view hierarchy programmatically, without using a nib.
/*
- (void)loadView {
}
*/
- (void)switchToNavigationMapButtons {
self.navigationItem.leftBarButtonItem = cancelButton;
self.navigationItem.rightBarButtonItem = acceptButton;
}
- (void)switchToMapInteractionButtons {
[self.navigationItem setHidesBackButton:YES];
[self.navigationItem setRightBarButtonItem:cancelMapEntryButton animated:YES];
//self.navigationItem.leftBarButtonItem = cancelMapEntryButton;
//self.navigationItem.rightBarButtonItem = clearMapEntryButton;
}
- (void)createLocationButtons {
coordinateSelectionImage = [UIImage imageNamed:@"location.png"];
updateLocationButton = [[UIBarButtonItem alloc] initWithImage:coordinateSelectionImage style:UIBarButtonItemStyleBordered target:self action:@selector(findCurrentLocationClicked)];
UIActivityIndicatorView* activityIndicator = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease];
[activityIndicator startAnimating];
findingLocationButton = [[UIBarButtonItem alloc] initWithCustomView:activityIndicator];
findingLocationButton.style = UIBarButtonItemStyleDone;
//[activityIndicator release];
[coordinateSelectionImage release];
}
- (void)switchToFindingLocationButton {
NSArray* buttons = [[NSArray alloc] initWithObjects:findingLocationButton,nil];
[self.toolbar setItems:buttons animated:YES];
[buttons release];
}
- (void)switchToUpdateLocationButton {
NSArray* buttons = [[NSArray alloc] initWithObjects:updateLocationButton,nil];
[self.toolbar setItems:buttons animated:YES];
[buttons release];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancel)];
acceptButton = [[UIBarButtonItem alloc] initWithTitle:@"Accept" style:UIBarButtonItemStyleDone target:self action:@selector(accept)];
cancelMapEntryButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelMapSelect)];
[self switchToNavigationMapButtons];
self.navigationItem.title = @"Choose Location";
self.locationManager = [[[CLLocationManager alloc] init] autorelease];
self.locationManager.delegate = self;
[self createLocationButtons];
[self switchToUpdateLocationButton];
if ( self.existingAddress != nil ) {
[self addAnnotationAtLocation:self.existingCoordinate withAddress:self.existingAddress];
self.selectedAddress = self.existingAddress;
self.selectedCoordinate = self.existingCoordinate;
}
}
- (void)addAnnotationAtLocation:(CLLocationCoordinate2D)coordinate withAddress:(NSString*)address {
if ( self.currentAnnotation != nil ) {
[mapView removeAnnotation:currentAnnotation];
}
currentAnnotation = [EventLocationMapAnnotation annotationWithCoordinate:coordinate];
currentAnnotation.address = address;
MKCoordinateRegion region;
region.center = coordinate;
region.span.longitudeDelta = 0.15f;
region.span.latitudeDelta = 0.15f;
[self.mapView setRegion:region animated:YES];
[self.mapView addAnnotation:currentAnnotation];
self.addressSearchBar.text = address;
}
- (void)findGeocodedSelectedLocation:(CLLocationCoordinate2D)coordinate {
MKReverseGeocoder* reverseGeocoder = [[MKReverseGeocoder alloc] initWithCoordinate:coordinate];
reverseGeocoder.delegate = self;
[reverseGeocoder start];
NSLog(@"Geocoding");
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)findCurrentLocationClicked {
[self switchToFindingLocationButton];
[self.locationManager startUpdatingLocation];
}
#pragma mark -
#pragma mark Actions
- (void)accept {
[self.delegate didAcceptLocationSelection:self.selectedCoordinate withAddress:self.selectedAddress];
}
- (void)cancel {
[self.delegate didCancelLocationSelection];
}
- (void)cancelMapSelect {
[self switchToNavigationMapButtons];
}
#pragma mark -
#pragma mark Begin Search Bar Delegate methods
- (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar {
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
//[self switchToNavigationMapButtons];
[self.navigationController setNavigationBarHidden:NO animated:YES];
if ( forwardGeocoder == nil ) {
forwardGeocoder = [[BSForwardGeocoder alloc] initWithDelegate:self];
}
[forwardGeocoder findLocation:searchBar.text];
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
//[self switchToMapInteractionButtons];
[self.navigationController setNavigationBarHidden:YES animated:YES];
[searchBar setShowsCancelButton:YES animated:YES];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
[self.navigationController setNavigationBarHidden:NO animated:YES];
//[searchBar resignFirstResponder];
}
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
// wait until location was updated
return YES;
}
#pragma mark -
#pragma mark CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
// todo: geocode this result
self.selectedCoordinate = newLocation.coordinate;
[self findGeocodedSelectedLocation:self.selectedCoordinate];
[self addAnnotationAtLocation:self.selectedCoordinate withAddress:@"Searching for address..."];
[locationManager stopUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
NSLog(@"There was an error %@", error.localizedDescription);
[self switchToUpdateLocationButton];
[locationManager stopUpdatingLocation];
}
#pragma mark -
#pragma mark Geolocation
-(void) displayGeocodingResultOptions:(NSArray*)results {
UITableAlert* alert = [[UITableAlert alloc] initWithTitle:@"Did you mean:" message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
alert.delegate = self;
alert.alertDelegate = self;
[alert show];
[alert release];
}
-(void)updateLocation:(BSKmlResult*)place {
// Zoom into the location
[mapView setRegion:place.coordinateRegion animated:TRUE];
NSString* name = place.address;
if ( name == nil ) {
name = addressSearchBar.text;
}
self.selectedAddress = name;
self.selectedCoordinate = [place coordinate];
[self addAnnotationAtLocation:place.coordinate withAddress:name];
}
#pragma mark -
#pragma mark UITableAlert
- (void)didPresentAlertView:(UIAlertView *)alertView {
if ( forwardGeocoder.results ) {
NSMutableArray* results = [[NSMutableArray alloc] init];
int resultSize = [forwardGeocoder.results count];
for ( int i = 0; i < resultSize; ++i ) {
BSKmlResult* place = [forwardGeocoder.results objectAtIndex:i];
[results addObject:place.address];
}
id data = [[UITableAlertDataSource alloc] initWithArray:results];
((UITableAlert *)alertView).tableData = data;
[results release];
}
}
-(void)tableAlert:(UITableAlert*)tableAlert didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
int row = indexPath.row;
if ( forwardGeocoder.results && [forwardGeocoder.results count] > row ) {
BSKmlResult* place = [forwardGeocoder.results objectAtIndex:row];
[self updateLocation:place];
}
[tableAlert dismissWithClickedButtonIndex:0 animated:YES];
}
#pragma mark -
#pragma mark BSForwardGeocoder
-(void)forwardGeocoderFoundLocation {
if(forwardGeocoder.status == G_GEO_SUCCESS)
{
// Dismiss the keyboard
[addressSearchBar resignFirstResponder];
if([forwardGeocoder.results count] == 1)
{
BSKmlResult *place = [forwardGeocoder.results objectAtIndex:0];
[self updateLocation:place];
}
else
{
[self displayGeocodingResultOptions:forwardGeocoder.results];
}
}
}
-(void)forwardGeocoderError:(NSString *)errorMessage {
NSLog(errorMessage);
}
#pragma mark -
#pragma mark MKReverseGeocoder
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error {
NSLog(@"Reverse geocoding failed: error given %@", [error userInfo]);
selectedAddress = [NSString stringWithFormat:@"%.2f,%.2f", selectedCoordinate.latitude, selectedCoordinate.longitude];
[self switchToUpdateLocationButton];
}
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark {
NSMutableString* buffer = [[NSMutableString alloc] initWithCapacity:64];
if ( placemark.thoroughfare ) {
[buffer appendString:placemark.thoroughfare];
[buffer appendString:@" "];
}
if ( placemark.subThoroughfare ) {
[buffer appendString:placemark.subThoroughfare];
[buffer appendString:@" "];
}
if ( placemark.locality ) {
[buffer appendString:placemark.locality];
[buffer appendString:@" "];
}
if ( placemark.subLocality ) {
[buffer appendString:placemark.subLocality];
[buffer appendString:@" "];
}
if ( placemark.administrativeArea ) {
[buffer appendString:placemark.administrativeArea];
[buffer appendString:@" "];
}
if ( placemark.postalCode ) {
[buffer appendString:placemark.postalCode];
[buffer appendString:@" "];
}
if ( placemark.country ) {
[buffer appendString:placemark.country];
[buffer appendString:@" "];
}
if ( placemark.countryCode ) {
[buffer appendString:placemark.countryCode];
[buffer appendString:@" "];
}
self.selectedAddress = buffer;
[self addAnnotationAtLocation:self.selectedCoordinate withAddress:buffer];
[self switchToUpdateLocationButton];
[buffer release];
}
#pragma mark -
#pragma mark MKMapViewDelegate
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
MKPinAnnotationView* view = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"address"];
if ( view == nil ) {
view = [[[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:@"address"] autorelease];
}
if ( [self.delegate isStartingPoint] ) {
[view setPinColor:MKPinAnnotationColorGreen];
} else {
[view setPinColor:MKPinAnnotationColorRed];
}
[view setCanShowCallout:YES];
[view setAnimatesDrop:YES];
return view;
}
#pragma mark -
#pragma mark Deallocation
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[addressSearchBar release];
[mapView release];
[cancelButton release];
[acceptButton release];
[cancelMapEntryButton release];
[forwardGeocoder release];
[existingAddress release];
[currentAnnotation release];
[selectedAddress release];
[toolbar release];
[super dealloc];
}
@end