7 min read

Core Location framework – an overview

Core Location implements all the three methods of location detection: GPS, Wi-Fi, and Cell Tower Triangulation. The developer can control location detection by only specifying the accuracy needed. Core Location then decides internally on which approach to use for actual location detection.

When creating an application that uses the Core Location framework, you need to first add it to your project in Xcode and include the <CoreLocation/Corelocation.h> header files in your .m or .h file.

The new iOS SDK 5 and Xcode 4.2 includes a nifty location simulation and debugging capability. Now we will understand how the location debugging feature helps you test your app by using different location values when your app is actually running in the simulator or a connected device.

Time for action – location debugging

Open the Hello Location application and run the application.

  1. If you are running your app on the iOS Simulator, then go to the Debug | Location Menu option where you can simulate multiple location inputs for your app, as shown in the following screenshot:
  2. If you are running your app on a connected device, you need to go to the Product | Debug | Simulate Location menu option in the Xcode 4 menu bar.
  3. Try changing to different locations and testing the app by clicking the Detect Location button in the app.

Location data is reported in your application via the Core Location’s Delegate object, CLLocationManagerDelegate. Based on the location service type used in your app, the corresponding Core Location Delegate function has to be implemented by your application to catch the appropriate location change event. We will look at it as we inspect the different Core Location Services.

What just happened?

We simulated location information on our iPhone, using the new feature of “Location Simulation” in iOS 5 SDK and Xcode 4.2. This new feature helps us analyze our app’s behavior in different locations. In the preceding example, we changed the location values one-by-one and clicked on the Detect Location button to echo the geo co-ordinates of our labels. iOS 5 Location simulation includes significant location updates, region monitoring, and continuous location updates via the GPX file support (a GPX file is an XML file format that contains a sequence of Geo Coordinates, typically for Tours or Navigational purposes).

Core location services

The Core Location framework provides the following services:

  • Standard location
  • Significant change
  • Region monitoring
  • Geocoding and reverse Geocoding – CLGeocoder (Added in iOS 5 SDK)
  • Direction using heading information

Standard location

Standard Location is the latitude and longitude information retrieved from Core Location. The Core Location Manager (the CLLocationManager object in the iOS SDK) returns this information in the CLLocation object.

NSString *latitudeTextData = [[NSString alloc]initWithFormat: @"%g",locMgr.location.coordinate.latitude];

Here the location object is an instance of the CLLocation object that contains the latitude and longitude variables. Standard location service with the Core Location Manager is started with the startUpdatingLocation function. You can tell the Location Manager to stop updating the location with the stopUpdatingLocation function.

distanceFilter and desiredAccuracy are two properties that define how often you will receive the location updates and how much accuracy (in meters) is required by your app.

With distanceFilter, you will receive location information if the device has moved distance equal to or more than the value specified in the distanceFilter property.

Accuracy of the location detections can be chosen from the following desiredAccuracy values:

Table 1

Use/Try to use the lowest accuracy possible (the lowest accuracy your application can work with) to avoid more battery power consumption.

Significant change

With the Core Location framework, you can also request for location updates having significant location value changes only. This method provides excellent power saving options, as well as the ability of the device to send location updates even when your application is not running. This method uses Cellular Radio to detect the device location.

To use the significant change location service in your app, you need to use the startMonitoringSignificantLocationChanges and stopMonitoringSignificantLocationChanges functions.

Core Location framework caches the location data, it is a good idea to get the timestamp on the measurement objects to make sure your application receives the correct and updated location information.

Region monitoring

With the Region monitoring services, you can define geographical boundary-based tracking for your apps. Consider a simple example of a Weather app that can use Region Monitoring to detect the user’s location based on physical boundaries and alert them if they cross a particular boundary, for example, if a user crosses a San Francisco city boundary towards San Jose, the app can trigger a boundary alert for the user and show him the new San Jose weather information.

The startMonitoringForRegion and stopMonitoringForRegion methods of the location framework are used to start and stop region monitoring in your application. Boundary entering and exiting are monitored by locationManager:didEnterRegion and locationManager:didExitRegion. Boundary crossing deteci on also requires an accuracy factor to determine the crossing factor needed to trigger the alert. This is done by the startMonitoringForRegion:desiredAccuracy method.

As with the Significant Change service, Region monitoring also works even if your application is not running. The most important part is that you need to register the Regions to be monitored with the device using the monitoredRegions property.

Use smart programming techniques to shut down location services when not required in order to conserve battery power. Another good idea is to turn off location if accuracy does not improve over a course of time.

Geocoding and reverse Geocoding – CLGeocoder

The CLGeocoder along with the CLPlacemark object provide the Geocoding and Reverse Geocoding functions in the Core Location framework. Note that these are new APIs added in the iOS 5.0 SDK.

The MKReverseGeocoder from the MapKit Framework has been deprecated. The CLGeocode object now handles the same. CLGeocode features as follows:

  • Requests are asynchronous and support only one operation per request
  • Supports multiple languages
  • Supports Forward and Reverse Geocoding
  • Does not require results to be displayed on a map
  • Worldwide coverage

Geocoding is done by any of the following three methods:

  1. geocodeAddressString:completionHandler: Geocodes a simple string, for example, Mountain View, San Francisco.
  2. geocodeAddressString:inRegion:completionHandler: Geocodes a specified string using regional information. Think of this as searching for the String Market Street in region San Francisco.
  3. geocodeAddressDictionary:completionHandler: Geocodes is a speci? ed address dictionary. This is a more structured geocoding request, usually providing the Address Street, Address City, and Address State fields in the AddressBook format. The following is an example code snippet for this function:
  4. CLGeocoder *geocoder =[[CLGeocoder alloc]init]; NSDictionary *address=[NSDictionary dictionaryWithObjectsAndKeys: @"32 Lincoln RoadRoad",kABPersonAddressStreetKey, @"Birmingham",kABPersonAddressCityKey,nil ]; [geocoder geocodeAddressDictionary:address completionHandler:^(NSArray *placemarks, NSError *error) { for(CLPlacemark *placemark in placemarks) { NSLog(@"Placemark %@",placemark); } }];

Don’t forget to add the AddressBook framework in build phases in Xcode and import the header files required in your Hello_LocationViewController.m file from the Hello World example.

#import <AddressBook/AddressBook.h> #import <AddressBook/ABPerson.h>

You can find the code at the book’s page at http://www.packtpub.com/iphone-location-aware-apps-beginners-guide/book – in a project called Hello Location – Geocode. Run the application and click on the Detect Location button and observe the Debug Console in Xcode; you should see an output as follows:

Placemark 32 Lincoln Road, Solihull, England, B27 6, United Kingdom @ <+52.44378245,-1.81094734> +/- 100.00m

This is the result of the geocoding, along with the accuracy of 100 meters.

Reverse Geocoding is handled by the reverseGeocodeLocation:completionHandler method in the CLGeocoder class.

The CLPlaceMark object is returned for both Forward and Reverse Geocoding.

Direction using heading

Heading information in the Core Location service signifies the direction in which the device is oriented. This information is very critical for augmented reality, navigation, and gaming applications. The direction in which a device is poini ng, reported by iOS devices with a magnetometer is known as heading, while direci on in which the iOS device is moving, reported by the GPS hardware, is known as course.

The CLHeading object holds the heading data reported by the Location Manager. The startUpdatingHeading method in the Location Manager is used to start the heading update process, while stopUpdatingHeading is used to stop it.

The CLHeading object contains the following properties:

Table 2

LEAVE A REPLY

Please enter your comment!
Please enter your name here