Question

I would be thankful to anyone, who could explain to me an algorithm of how one of these methods (or both) work:

I have two CLLocationCoordinate2Ds or two MKMapPoints - what calculations should I perform to calculate a distance beetween them with respect to real-world surface of earth? (Obviously, stand-alone calculation of Euclidean distance is not applicable to this task.)

Background: I want to know if a knowledge of the internals of these methods would help me to optimize some calculations involving large numbers of points on a MapKit map.

Was it helpful?

Solution

The wikipedia article on Geographical distance has some formulae to calculate geodesic distances.

Here is a piece of code I'm currently using that gave me acceptable results:

const float EarthRadius = 6378137.0f;

float SquaredGeodesicDistance(CLLocationCoordinate2D a, CLLocationCoordinate2D b)
{
    float dtheta = (a.latitude - b.latitude) * (M_PI / 180.0);
    float dlambda = (a.longitude - b.longitude) * (M_PI / 180.0);
    float mean_t = (a.latitude + b.latitude) * (M_PI / 180.0) / 2.0;
    float cos_meant = cosf(mean_t);

    return (EarthRadius * EarthRadius) * (dtheta * dtheta + cos_meant * cos_meant * dlambda * dlambda);
}

float GeodesicDistance(CLLocationCoordinate2D a, CLLocationCoordinate2D b)
{
    return sqrtf(SquaredGeodesicDistance(a, b));
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top