Have time for a little extention of simple RouteMe quide. Let’s add a user location finding.
Create find button (code+design)
- add “IBOutlet UIBarButtonItem *findmeBarButton;”
- add empty method “- (IBAction) startFinding”
- add “Find me” button in the IB of the main view
- connect the button with IBOutlet and “startFinding” IBAction
Core Location (interface)
- import “CoreLocation/CoreLocation.h”
- add CLLocationManagerDelegate to the interface
- add “CLLocationManager* locationManager;”
- and we will need some methods in a moment
- now the interface looks like:
#import "UIKit/UIKit.h"#import "RMMapView.h"#import "CoreLocation/CoreLocation.h" @interface RouteMeSourceSelectionViewController : UIViewController<rmmapviewdelegate, cllocationmanagerdelegate="" uipickerviewdatasource,="" uipickerviewdelegate,="">{ IBOutlet RMMapView *mapView; IBOutlet UIPickerView *mapSourcePicker; IBOutlet UIBarButtonItem *mapSettingsBarButton; IBOutlet UIBarButtonItem *findmeBarButton; CLLocationManager* locationManager;} - (IBAction) showMapsSettings;- (IBAction) startFinding;- (void) stopFinding; - (void) showFinding;- (void) hideFinding; @end
sorry it’s copied with some errors. It’s better to copy code from sources.
Core Location (code)
The first thing is to create and start/stop a location manager instance:
- (IBAction) startFinding{ if (findmeBarButton.style == UIBarButtonItemStyleBordered) { if (!locationManager) { locationManager = [[CLLocationManager alloc] init]; locationManager.delegate = self; locationManager.distanceFilter = 10; // 1000 = kilometer locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; } [self showFinding]; [locationManager startUpdatingLocation]; NSLog (@"Locating started"); } else [self stopFinding];} - (void) stopFinding{ [locationManager stopUpdatingLocation]; [self hideFinding]; NSLog (@"Locating stopped");}
When Finding is on we change the title and illuminate the FindMeButton, and vice versa. These methods make this and some other GUI changes:
- (void)showFinding{ [findmeBarButton setTitle:@"Finding..."]; [findmeBarButton setStyle: UIBarButtonItemStyleDone]; [mapSettingsBarButton setEnabled:NO];} - (void)hideFinding{ [findmeBarButton setTitle:@"Find Me"]; [findmeBarButton setStyle: UIBarButtonItemStyleBordered]; [mapSettingsBarButton setEnabled:YES];}
Similarly hide/show “findmeBarButton” when show/hide the maps’s settings: “[findmeBarButton setEnabled:!findmeBarButton.enabled];”
At least we need to implement location protocol methods: show message if any error and show place on the map when location is found. Make maps’s zoom depend of found location’s accuracy.
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{ [mapView.contents moveToLatLong:newLocation.coordinate]; if([newLocation horizontalAccuracy] > 1000) { if([newLocation horizontalAccuracy] > 10000) [mapView.contents setZoom:6]; else if([newLocation horizontalAccuracy] <= 10000 && [newLocation horizontalAccuracy] > 5000) [mapView.contents setZoom:9]; if([newLocation horizontalAccuracy] <= 5000 && [newLocation horizontalAccuracy] > 1000) [mapView.contents setZoom:12]; [self showFinding]; } else if([newLocation horizontalAccuracy] <= 1000 && [newLocation horizontalAccuracy] >= 0) { [mapView.contents setZoom:16]; [self stopFinding]; }} - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{ [self stopFinding]; UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle: @"Location not found" message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [errorAlert show]; [errorAlert release];}
This sample is a part of my real application - Meeting Point. It’s free to download, try it.