Skip to content

Instantly share code, notes, and snippets.

@ldandersen
Created September 6, 2011 22:40
Show Gist options
  • Save ldandersen/1199205 to your computer and use it in GitHub Desktop.
Save ldandersen/1199205 to your computer and use it in GitHub Desktop.
Great circle distance in Cocoa
static NSInteger STEarthRadiusKilometers = 6371;
- (NSNumber *)distanceFromLatitude:(NSDecimalNumber *)inLatitude longitude:(NSDecimalNumber *)inLongitude;
{
CLLocationDegrees inLatitudeDegrees = [inLatitude doubleValue];
CLLocationDegrees latitudeDegrees = [self.latitude doubleValue];
CLLocationDegrees inLongitudeDegrees = [inLongitude doubleValue];
CLLocationDegrees longitudeDegrees = [self.longitude doubleValue];
double distance = acos(sin(inLatitudeDegrees) * sin(latitudeDegrees) + cos(inLatitudeDegrees) * cos(latitudeDegrees) * cos(longitudeDegrees - inLongitudeDegrees)) * STEarthRadiusKilometers;
return [NSNumber numberWithDouble:distance];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment