Sensing Location

Location sensing is achieved using the Core Location framework. The framework determines the position and heading using the hardware available on the device. This can include a GPS chip, a Wi-Fi antenna, a gyroscope, and an accelerometer. This hardware layer is abstracted from us and we are simply given the location with an accuracy level.

Core Location has no user interface, so it is up to you to represent the retrieved data in a meaningful way to your application.

Core Location is not included by default in a new Xcode project. You need to make sure to properly link to the CoreLocation.framework in the Build Phases tab of your project settings if you want to use any classes starting with the CL prefix.

Location Data

The basic location data object is CLLocation.

CLLocation incorporates the geographical coordinates and altitude of the device’s location along with values indicating the accuracy of the measurements and when those measurements were made. It also includes information about the speed and heading in which the device is moving.

The coordinate property is a struct holding a latitude and a longitude. Both are of type CLLocationDegrees, which is just a double. The value is given in degrees.

The accuracy of the measurement can be read using the horizontalAccuracy property, of type CLLocationAccuracy, which is just a double. The value is given in meters.

The altitude property holds the altitude value. Its accuracy can be read using the verticalAccuracy property. The value is given in meters. A negative value means below sea level.

The course property indicates the direction in which the device is moving, of type CLLocationDirection, which is also just a double. The value is given in degrees, with North at 0 going clockwise (East is 90 degrees). If the value is negative, it means the direction is invalid.

The speed property indicates the speed at which the device is moving in its current direction. It is of type CLLocationSpeed, which is a double, and the value is given in meters per second. If the value is negative, it means the speed is invalid.

Finally, the timestamp indicates the time at which all these values were determined. It is of type NSDate.

CLLocation also includes the distanceFromLocation: method, which returns the distance between it and another location. The value is of type CLLocationDistance, which is a double, and is given in meters.

Location Manager

You will usually not build your own CLLocation objects, but retrieve them from a CLLocationManager.

You can access properties directly from the location manager, but it’s usually a better idea to use delegation. Instead of polling the CLLocationManager constantly, we can conform to the CLLocationManagerDelegate protocol and have it notify us when new data is ready.

The first step is to check if the user has enabled location monitoring for their device.

You can then create an instance of the location manager, and tell it to begin sensing for location.

The first time your app requests to use location services, a pop up will appear asking the user to do so. You can add a little explanation to this by setting the purpose property.

Using the device’s location sensors is one of the biggest battery draining operation you can do. It’s a good idea to only turn on location sensing when the view is visible.

You can set the desiredAccuracy property as low as you need it to be to save power. Possible values are:

  • kCLLocationAccuracyBestForNavigation
  • kCLLocationAccuracyBest
  • kCLLocationAccuracyNearestTenMeters
  • kCLLocationAccuracyHundredMeters
  • kCLLocationAccuracyKilometer
  • kCLLocationAccuracyThreeKilometers

Once this is set, the device does its best using:

  • Cellular tower triangulation – not very accurate, but uses minimum power
  • Wi-Fi node database lookup – more accurate and requires more power
  • GPS – very accurate, but requires lots of power

The distanceFilter property can be set to only trigger events once there is a minimum change in position. The value is set in meters.

Once the manager is set up, we can get the location using delegate methods.

  1. Core Location Framework Reference. Apple, Oct 12 2011.
  2. Paul Hegarty. Lecture 11 Slides. iPad and iPhone Application Development. Stanford, Nov 28 2011.

Comments are closed.