Sean Carpenter

CLLocation Extensions

I’ve been working on a new iOS project that uses CoreLocation to determine some points. While CLLocation has a method for determining the distance between two locations (distanceFromLocation:) it doesn’t have one for doing the opposite: calculating a second location based on a distance (and bearing) from the first.

So I added a category on CLLocation that performs this calculation:

1
2
3
4
5
6
#import <CoreLocation/CoreLocation.h>
#import "CLLocation+LocationExtensions.h"

CLLocation* startPoint = [[CLLocation alloc] initWithLatitude:39.90657 longitude:-75.16656];
CLLocation* newPoint = [startPoint locationAtDistance:300 andBearing:180];
NSLog(@"New point: %@", newPoint); // new point is at 39.90387204, -75.16656

The other need I had was to calculate the bearing between two points:

1
2
3
4
5
6
7
#import <CoreLocation/CoreLocation.h>
#import "CLLocation+LocationExtensions.h"

CLLocation* startPoint = [[CLLocation alloc] initWithLatitude:39.90657 longitude:-75.16656];
CLLocation* endPoint = [[CLLocation alloc] initWithLatitude:39.90387204 longitude:-75.16656];
CLLocationDegrees bearing = [startPoint bearingTo:endPoint];
NSLog(@"Bearing: %f", bearing); // bearing is 180

Both of these calculations use the formulas provided on the page Calculate distance, bearing and more between Latitude/Longitude points. This page has lots of information on the formulas and provides them in mathematical notation as well as examples translated to Javascript.

My Objective-C category incorporating these calculations is available from my Github account.