From the humble beginnings of the mobile device era, telecommunication providers and application developers have had the ability to geo-locate a device. Early not-so-smart devices used triangulation – more specifically, trilateration – to pinpoint a user’s location. As hardware and supporting technologies evolved, satellite-based navigation capabilities were added to mobile devices. Additionally, every current Apple mobile device includes an accelerometer for measuring speed and a magnetometer for determining a device’s heading.

Apple’s iOS 6 introduced developers to a much-improved Core Location framework (PDF). The new framework takes full advantage of current hardware to provide a robust suite of location-based services. While trilateration or global positioning alone provides reasonable accuracy, Apple uses several techniques in unison to provide accurate location coordinates of the device. To fully appreciate the complexity of iOS location-based services, we need to understand the science and mathematics behind trilateration, assisted global positioning, and Apple’s use of crowd-sourced Wi-Fi.

## Trilateration

Triangulation and trilateration are two mathematical processes for determining the location of a point. Triangulation uses a process of measuring angles from known locations – such as cell towers – to calculate current position. Trilateration determines the position of a device by calculating the intersection of circles or spheres representing the distance of a device from known locations. The accuracy of these approaches is improved as more fixed locations are used in the calculations.

Signal strength is used to predict the distance of a device from various cell towers. With the cell tower’s fixed location known, a distance radius is established. Without the intersection of circles from a second or third cell tower position, we can only determine that the device is located somewhere on the circumference of the distance circle (Figure A). An Omni-directional antenna on the cell tower can narrow down the position of a mobile device, but not close enough to satisfy the requirements of today’s iOS apps.

#### Figure A

As cellular-enabled mobile devices move about, it is common – especially in densely populated areas – for the device to communicate simultaneously with several cell towers. If two cell towers are in communication with the device, we can derive two possible locations from the two intersecting points (Figure B). The best case, however, is to leverage the information from at least three cell towers to pinpoint a location represented by the single intersection of three circles or spheres (Figure C).

#### Figure C

Global navigation systems, like GPS, provide the ability to pinpoint a device by using known coordinates of satellites as opposed to cell towers. Trilateration is used to find the intersecting point of spheres to determine the location of a GPS-enabled mobile device. GPS hardware is energy demanding, and can quickly drain the battery of a mobile device. Apple uses the more efficient Assisted Global Positioning (AGPS) process for locating and linking to satellites. With AGPS, satellite positioning is retrieved from a cellular or Wi-Fi connection reducing the amount of time it takes to discover a satellite.

## Crowd-Sourcing

Apple leverages crowd-sourced information to (1) fine tune the accuracy of location-based services on cellular and GPS enabled devices, and (2) enable location-based services on non-cellular or Wi-Fi only devices.

Crowd-sourced Wi-Fi is by far the most innovative approach for determining the location of a mobile device. Apple uses a database of Wi-Fi hotspots and cell tower locations – submitted anonymously by any number of iOS mobile devices – to help determine the coordinates of a single mobile device. Cellular, AGPS, and crowd-sourced Wi-Fi information are used to feed accurate data to iOS location-based services. Apple’s iOS Core Location framework makes it possible to perform complex tasks with only a few lines of code.

## Using Location-Based Services

The iOS location-based services provide simple methods for determining a device’s current location. The first step in using location-based services is to add the Core Location Framework to your iOS project. With the framework linked, you can access the standard location service, significant-change service, and region monitoring capability within your app.

#### Figure D

Choose the “Summary” tab and scroll down to the Linked Frameworks and Libraries section.

Click the “+” button, select CoreLocation, and click “Add.”

With the Core Location Framework linked to your project, add an #import reference for the <CoreLocation/CoreLocation.h> header file in the code files where the location manager object will be created and initialized. Typically, there is only one instance of the location manager object. For this reason, it is common for developers to create the location manager object in the App Delegate implementation file. The App Delegate header file (.h) should resemble Code A.

#### Code A

`#import <UIKit/UIKit.h>`
`#import <CoreLocation/CoreLocation.h>`
`@interface AppDelegate : UIResponder <UIApplicationDelegate, CLLocationManagerDelegate>`
`@property (strong, nonatomic) UIWindow *window;`
`@property (strong, nonatomic) CLLocationManager *locationManager;`
`@end`

The App Delegate implementation file (.m) is where the location manager object will be created. We synthesize the property, and then create/initialize our object within the method application didFinishLaunchingWithOptions as shown in Code B. If location services are available, we initialize the object.

#### Code B

`#import "AppDelegate.h"`
`@implementation AppDelegate`
`@synthesize locationManager = _locationManager;`
`- (BOOL)application:(UIApplication *)application     didFinishLaunchingWithOptions:(NSDictionary *)launchOptions`
`{`
`if(self.locationManager == nil){`
`_locationManager=[[CLLocationManager alloc] init];`
`_locationManager.distanceFilter = kCLDistanceFilterNone;`
`_locationManager.desiredAccuracy = kCLLocationAccuracyBest;`
`_locationManager.delegate = self;`
`self.locationManager = _locationManager;`
`}`
`if([CLLocationManager locationServicesEnabled]){`
`[self.locationManager startUpdatingLocation];`
`}`
`return YES;`
`}`
`@end`

With the Core Location framework linked, and the location manager object initialized, compile the app to run in the iPhone Simulator. As you can see, the user is prompted to allow location services for this particular app (Figure E).

#### Figure E

Location-based services are being used to retrieve the current location of a mobile device for social media and marketing applications. Location-based services on iOS 6 offer the ability to monitor shape-based regions and provide real-time feedback for triggering notifications when a device crosses boundaries. Retailers are using geo-fencing strategies in their apps to spawn a notification to a user when he or she is in close proximity to a predefined location – such as a coffee shop.

The Reminders app in iOS 6 uses region monitoring to support location-based reminders. This capability is available for use within any iOS 6 app. The Location Awareness Programming Guide is a great resource for detailed information regarding the standard location service, significant-change location service, and the improved region monitoring capabilities. Apple has distilled a very complex location-deriving technology into a framework that will definitely inspire many great new apps.