Created
May 27, 2012 15:56
-
-
Save neilinglis/2814861 to your computer and use it in GitHub Desktop.
Rough Haversine Formula
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
#import "CLLocation+NHIAdditions.h" | |
@implementation CLLocation (NHIAdditions) | |
- (CGFloat)distanceToPoint:(CLLocation*)pointB | |
{ | |
//uses the haversine formula. http://en.wikipedia.org/wiki/Haversine_formula | |
NSInteger nRadius = 6371; // Earth's radius in Kilometers | |
// Get the difference between our two points then convert the difference into radians | |
CGFloat differenceInLatitudes = (pointB.coordinate.latitude - self.coordinate.latitude) * (M_PI/180); | |
CGFloat differenceInLongitudes = (pointB.coordinate.longitude - self.coordinate.longitude) * (M_PI/180); | |
CGFloat pointALatitudeInRadians = degreesToRadian(self.coordinate.latitude); | |
CGFloat pointBLatitudeInRadians = degreesToRadian(pointB.coordinate.latitude); | |
CGFloat nA = pow ( sin(differenceInLatitudes/2), 2 ) + cos(pointALatitudeInRadians) * cos(pointBLatitudeInRadians) * pow ( sin(differenceInLongitudes/2), 2 ); | |
CGFloat nC = 2 * atan2( sqrt(nA), sqrt( 1 - nA )); | |
CGFloat distance = nRadius * nC; | |
return distance; // Return our calculated distance | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment