Use Current Location Service in Map

Posted in code with : ios


Location Service Authorization

1. Set info.plist

For iOS SDK 8.0 and later, we need to set NSLocationWhenInUseUsageDescription and ‘NSLocationAlwaysUsageDescription’ in info.plist file. A sample case is:

1 <key>NSLocationWhenInUseUsageDescription</key>
2 <string>Do you allow the app to use your location?</string>
3 <key>NSLocationAlwaysUsageDescription</key>
4 <string>Are you willing to allow the app to use your location?</string>

The string will appear in the popup dialog. You can leave it as empty, and only the system message will appear in the popup.

2. Ask the User for Location Service Authorization

We ask for the authorization by an instance of CLLocationManager.

 1 if ([CLLocationManager locationServicesEnabled] ) { // check if the location service is enabled for the device
 2     if (self.locationManager == nil ) {
 3         self.locationManager = [[CLLocationManager alloc] init];
 4         self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
 5         self.locationManager.distanceFilter = kCLDistanceFilterNone;
 6     }
 7     // Check for iOS 8. Without this guard the code will crash with "unknown selector" on iOS 7.
 8     if([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]){
 9         CLAuthorizationStatus locationAuthorizationStatus = [CLLocationManager authorizationStatus];
10         if (locationAuthorizationStatus == kCLAuthorizationStatusNotDetermined && ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)] || [self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])) {
11             // choose one request according to your business.
12             if([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationAlwaysUsageDescription"]){
13                 [self.locationManager requestAlwaysAuthorization];
14             } else if([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationWhenInUseUsageDescription"]) {
15                 [self.locationManager  requestWhenInUseAuthorization];
16             } else {
17                 NSLog(@"Info.plist does not contain NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription");
18             }
19         }
20     }
21 }

Get Location

For iOS SDK 8.0 and later, we have two ways to get user current location:

  1. set showsUserLocation to YES for map view.
  2. send method startUpdatingLocation to CLLocationManager instance.

1. showsUserLocation

For this way, after we set showsUserLocation to YES,

 1 - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
 2 {
 3     // Center the map the first time we get a real location change.
 4     static dispatch_once_t centerMapFirstTime;
 5     
 6     if ((userLocation.coordinate.latitude != 0.0) && (userLocation.coordinate.longitude != 0.0)) {
 7         dispatch_once(&centerMapFirstTime, ^{
 8             [self.mapView setCenterCoordinate:userLocation.coordinate animated:YES];
 9         });
10     }
11     
12 }

Before implementing the above two methods, we should set the map view’s delegate comfirm with MapViewDelegate protoco.

2. Send message startUpdateingLocation to CLLocationManager Instaance

For this way, after we send the method,

 1 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
 2 {
 3     self.currentLocation = [locations lastObject];
 4     CLLocationCoordinate2D currentCoordinate = self.currentLocation.coordinate;
 5     
 6     if (self.currentLocationAnnotation) {
 7         [self.mapView removeAnnotation:self.currentLocationAnnotation];
 8     }
 9     self.currentLocationAnnotation = [[CurrentLocationAnnotation alloc] initWithCoordinate:currentCoordinate];
10     [self.mapView addAnnotation:self.currentLocationAnnotation];
11 }
1 - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
2 {
3     NSLog(@"didFailWithError: %@", error);
4     UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
5     [errorAlert show];
6 }

Before implementing the above methods, set the CLLocationManager’s delegate confirm with CLLocationManagerDelegate protoco.

References

reference: