Getting iPad Location Information using the iOS 6 Core Location Framework

From Techotopia
Jump to: navigation, search
PreviousTable of ContentsNext
An Example iOS 6 iPad MKMapItem ApplicationAn Example iOS 6 iPad Location Application

Learn SwiftUI and take your iOS Development to the Next Level
SwiftUI Essentials – iOS 14 Edition book is now available in Print ($39.99) and eBook ($29.99) editions. Learn more...

Buy Print Preview Book

The iPad is able to employ a number of different techniques for obtaining information about the current geographical location of the device. These mechanisms include GPS, cell tower triangulation and finally (and least accurately), by using the IP address of available Wi-Fi connections. The mechanism that is used by iOS to detect location information is, however, largely transparent to the application developer and the system will automatically use the most accurate solution available at any given time. In fact, all that is needed to integrate location based information into an iOS 6 iPad application is an understanding of how to use the Core Location Framework which, incidentally, is the subject of this chapter.

Once the basics of location tracking with Core Location have been covered in this chapter, the next chapter will provide detailed steps on how to create An Example iOS 6 iPad Location Application.


The Basics of Core Location

The key classes contained within the Core Location framework are CLLocationManager and CLLocation. An instance of the CLLocationManager class is created within the application and a property set to indicate the level of location accuracy that is required by the application. The location manager is then instructed to start tracking location information:

CLLocationManager *locationMgr = 
      [[CLLocationManager alloc] init];
locationMgr.desiredAccuracy = kCLLocationAccuracyBest;
locationMgr.delegate = self;

[locationMgr startUpdatingLocation];

With each location update, an application delegate method named didUpdateToLocation is called by the location manager and passed information about the current location. The above code also, therefore, assigns the current class as the location manager’s delegate.

Configuring the Desired Location Accuracy

The level of accuracy to which location information is to be tracked is specified via the desiredAccuracy property of the CLLocationManager object. It is important to keep in mind when configuring this property that the greater the level of accuracy selected the greater the drain on the device battery. An application should, therefore, never request a greater level of accuracy than is actually needed.

A number of predefined constant values are available for use when configuring this property:

  • kCLLocationAccuracyBestForNavigation – Uses the highest possible level of accuracy augmented by additional sensor data. This accuracy level is intended solely for use when the device is connected to an external power supply.
  • kCLLocationAccuracyBest – The highest recommended level of accuracy for devices running on battery power.
  • kCLLocationAccuracyNearestTenMeters - Accurate to within 10 meters.
  • kCLLocationAccuracyHundredMeters – Accurate to within 100 meters.
  • kCLLocationAccuracyKilometer – Accurate to within one kilometer.
  • kCLLocationAccuracyThreeKilometers – Accurate to within three kilometers.

Configuring the Distance Filter

The default configuration for the location manager is to report updates whenever any changes are detected in the location of the device. The distanceFilter property of the location manager allows applications to specify the amount of distance the device location must change before an update is triggered. If, for example, the distance filter is set to 1000 meters the application will only receive a location update when the device travels 1000 meters or more from the location of the last update. For example, to specify a distance filter of 1500 meters:

locationMgr.distanceFilter = 1500.0f;

The distance filter may be cancelled, thereby returning to the default setting, using the kCLDistanceFilterNone constant:

locationMgr.distanceFilter = kCLDistanceFilterNone;

The Location Manager Delegate

Location manager updates and errors result in calls to two delegate methods defined within the CLLocationManagerDelegate protocol. Templates for the two delegate methods that must be implemented to comply with this protocol are as follows:

#pragma mark -
#pragma mark CLLocationManagerDelegate

-(void)locationManager:(CLLocationManager *)manager
   didUpdateToLocation:(CLLocation *)newLocation
   fromLocation:(CLLocation *)oldLocation
	// Handle location updates

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
	// Handle error

Obtaining Location Information from CLLocation Objects

Location information is passed through to the didUpdateLocation delegate method in the form of CLLocation objects. A CLLocation object encapsulates the following data:

  • Latitude
  • Longitude
  • Horizontal Accuracy
  • Altitude
  • Altitude Accuracy

Longitude and Latitude

Longitude and latitude values are stored as type CLLocationDegrees and may be obtained from a CLLocation object as follows:

CLLocationDegrees currentLatitude = location.coordinate.latitude;
CLLocationDegrees currentLongitude = location.coordinates.longitude;


Horizontal and vertical accuracy are stored in meters as CLLocationDistance values and may be accessed as follows:

CLLocation verticalAccuracy = location.verticalAccuracy;
CLLocation horizontalAccurcy = location.horizontalAccuracy;


The altitude value is stored in meters as type a CLLocationDistance value and may be accessed from a CLLocation object as follows:

CLLocation altitude = location.altitude;

Calculating Distances

The distance between two CLLocation points may be calculated by calling the distanceFromLocation method of the end location and passing through the start location as an argument. For example, the following code calculates the distance between the points specified by newLocation and oldLocation:

CLLocationDistance distance = [newLocation distanceFromLocation:oldLocation];

Location Information and Multitasking

Location based iPad applications are one of the three categories of application that are permitted to continue executing when placed into the background (for a detailed description of multitasking refer to An Overview of iOS 6 iPad Multitasking). If location updates are required when the application is in the background state it is strongly recommended that the desired accuracy setting be reduced within the applicationDidEnterBackground method by making a call to the startMonitoringSignificantLocationChanges method of the location manager object. This will ensure that the application is only notified of significant changes to the location of the device thereby reducing the load on the battery.


This chapter has provided an overview of the use of the iOS Core Location Framework to obtain location information within an iPad application. This theory will be put into practice in the next chapter entitled An Example iOS 6 iPad Location Application.

Learn SwiftUI and take your iOS Development to the Next Level
SwiftUI Essentials – iOS 14 Edition book is now available in Print ($39.99) and eBook ($29.99) editions. Learn more...

Buy Print Preview Book

PreviousTable of ContentsNext
An Example iOS 6 iPad MKMapItem ApplicationAn Example iOS 6 iPad Location Application