Hi
I get some GPS coordinates from Google Maps and I need to find the distance between them using Objective C. I have implemented the formula but I get results that are way to big.
I have tested the values from Google Maps by passing them back into Google Earth and a Geocoding service on the internet and everything checks out. Im now beginning to suspect that the cosine law demands I do some sort of conversion with the coordinates before I pass them in.
I did a similar implementation of the Haversine formula, but this also gave me to big results. I then switched to the cosine since it was easier to debug and I don't need very high precision.
Hope someone could shed a little light on this one, or use the code:)
- (CGFloat) calculateDistanceBetweenPoints:(CGPoint) origin andDestination:(CGPoint) destination {
//To convert kilometers to miles, divide by 1.609
// x = latitude
// y = longitude
/* example:
Dubai : 25.248665, 55.352917
Amsterdam : 52.309071, 4.763385
Approx dist: 5,182.62 KM
Calc. dist : 8,253.33
*/
CGFloat toRad = (M_PI / 180);
CGFloat R = 6371.0f; //earth's mean radius in Km
CGFloat sinePart = sinf( origin.x * toRad ) * sinf( destination.x * toRad );
CGFloat cosinePart = cosf( origin.x * toRad ) * cosf( destination.x * toRad );
CGFloat deltaCosinePart = cosf( ( destination.y - origin.y ) * toRad );
CGFloat delta = acosf( sinePart + cosinePart * deltaCosinePart) * R;
return delta;
}
Above calculated from links referenced here:stackoverflow question