Created
September 6, 2011 22:40
-
-
Save ldandersen/1199205 to your computer and use it in GitHub Desktop.
Great circle distance in Cocoa
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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