Thursday, September 20, 2012

iOS Core Location programming


Including the location feature in the iOS app can greatly benefit the usability of an iOS app. Below article gives and idea of how to use the iOS core location API.

1) First step is to import the Core location framework in the app using the statement #import  

2) Next is to create the instance of CLLocationManager and save in the app for future calls. Below statements will let a developer do this

locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;

3) Next step is to request CLLocationManager to startUpdating the location. Below steps will do this

[locationManager startUpdatingLocation];

4) Next step is to override the delegate methods for success and failure callbacks. Below are the two methods will let us do that

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    self.currentLocation = newLocation;
 }

&

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
}

Some notes on using the CoreLocation APIs is given below:

- How often the callback get called? This method will be called when Core Location has new location data for us to process. We receive these messages both when we have better accuracy for a current location, or the device has moved. These messages can also come in quickly one after the other.

- How to determine the accuracy of the user location: It is better to use the accuracy value to be able to properly determin the user location. Accuracy value of +/-100 value will be sufficient for the accuracy. Once we get the accurate location, the updation can be stopped. However, for apps host map, it may be required that we keep updating the location. In such cases, we may not be able to stop updating the location information.

newLocation.horizontalAccuracy <= 100.0f

- When all the errors will be called back? - error will be called back on multiple scenarios. Below are the major error codes and their descriptions: NOTE: Many of these error codes are only supported from iOS6.0 or later. So, extra care must be taken for relying on these error codes


kCLErrorLocationUnknown - if the location services is unavailable, right away it reports this error. However, it keep trying and hence, most of the cases, we can ignore this error
kCLErrorDenied - If the user denies your application’s use of the location service.  Upon receiving such an error, you should stop the location service.
kCLErrorHeadingFailure - If a heading could not be determined because of strong interference from nearby magnetic fields
kCLErrorNetwork - The network was unavailable or a network error occurred.
kCLErrorRegionMonitoringDenied - Access to the region monitoring service was denied by the user.
kCLErrorRegionMonitoringFailure - A registered region cannot be monitored
kCLErrorRegionMonitoringSetupDelayed - Core Location could not initialize the region monitoring feature immediately.
kCLErrorRegionMonitoringResponseDelayed - Core Location will deliver events but they may be delayed. Possible keys in the user information dictionary are described in “Error User Info Keys.”
kCLErrorGeocodeFoundNoResult - The geocode request yielded no result
kCLErrorGeocodeFoundPartialResult - The geocode request yielded a partial result.
kCLErrorGeocodeCanceled - The geocode request was canceled.
kCLErrorDeferredFailed - The location manager did not enter deferred mode for an unknown reason.
kCLErrorDeferredNotUpdatingLocation - The location manager did not enter deferred mode because location updates were already disabled or paused.
kCLErrorDeferredAccuracyTooLow - Deferred mode is not supported for the requested accuracy. The accuracy must be set to kCLLocationAccuracyBest or kCLLocationAccuracyBestForNavigation.
kCLErrorDeferredDistanceFiltered - Deferred mode does not support distance filters. Set the distance filter to kCLDistanceFilterNone.
kCLErrorDeferredCanceled - The request for deferred updates was canceled by a subsequent call. Requests to defer updates are processed asynchronously. This error is returned if you call the disallowDeferredLocationUpdates method or schedule a new deferred update before the previous deferred update request is processed.



- Is there a way to know whether the location service is enabled ? - Yes, there can be two possibilities, 1) Location services are disabled at the system level 2) Location services are disabled just for one app. In both these case, the API [CLLocationManager locationServicesEnabled];. Also, in the didFailWithError call back, check for the error code kCLErrorDenied. If we are using the map, below statements also will be useful
MKUserLocation *userLocation = map.userLocation;
BOOL isLocationAvailable = userLocation != nil;

- Do CLLocationManager make use of internet to find the location? Usually, CCLocationManager uses the GPS device on the device for location information.

- Is there any Apple Sample which we can refer for the Location based programming? - Yes. refer to the LocateMe sample http://developer.apple.com/library/ios/#samplecode/LocateMe/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007801

No comments:

Post a Comment