3

How do I get an accurate distance (in meters) given two geo-points (two latitude/longitude pair) ?

Possible Duplicates:

Distance Between Two GEO Locations

Calculating the distance of geo locations

Android calculate distance between two locations

How to find distance from the latitude and longitude of two locations?

Community
  • 1
  • 1
Manish Jain
  • 865
  • 3
  • 13
  • 28
  • 1
    I can't parse your question. Could you add some punctuation? – mik01aj Feb 03 '11 at 12:42
  • Possible duplicate of [How to find distance from the latitude and longitude of two locations?](https://stackoverflow.com/questions/1420045/how-to-find-distance-from-the-latitude-and-longitude-of-two-locations) – Cœur Jul 10 '18 at 16:46

3 Answers3

3

There's no distance measurement on the iPhone that will give you a resolution of 2 meters. You can use Core Location's -[CLLocation distanceFromLocation: otherLocation] method to get a displacement in meters between two locations, but bear in mind:

  • nowhere that I've seen does Apple explain what geode is used for their coordinates, and indeed whether it's the same geode for different reckonings of position
  • the model they use doesn't take altitude into account, which is pretty crappy for working out the distances between human-sized objects in a field-sized area. It's fine for reckoning the distance between London and Moscow, though - the error is small.
  • when your device isn't plugged in, using really-high precision location data combined with motion detection is going to completely suck the battery
  • without using motion detection, you can only tell where the device is to within tens of metres.
3

If you want to get distance from two coordinates you can use this snippet:

#include <math.h>
#define DEG2RAD(degrees) (degrees * 0.01745327)
#define RADIUS_OF_EARTH 6378.1

+ (float) getDistanceFromStartCoords:(CLLocationCoordinate2D)start andEndCoords:(CLLocationCoordinate2D)end 
{
    float dist = acos((cos(DEG2RAD(start.latitude))*
                 cos(DEG2RAD(end.latitude))*
                 cos((-1*DEG2RAD(end.longitude))-
                     (-1*DEG2RAD(start.longitude)))) +
              (sin(DEG2RAD(start.latitude))*
               sin(DEG2RAD(end.latitude)))) * 
            RADIUS_OF_EARTH;

    return dist;
}
elp
  • 8,021
  • 7
  • 61
  • 120
  • The world is not spherical. Your reckoning of position relative to the geoid has an error of up to 21km. –  Feb 03 '11 at 13:55
  • 1
    @Graham correct... but Manish Jain wont to check points on TWO METERS of distance!!! In two meters, the world fuse don't change! – elp Feb 03 '11 at 14:00
  • but my distance in CLLocation variable not in CLLocationCoordinate2D – Manish Jain Feb 03 '11 at 14:10
  • CLLocationCoordinate2D starting; starting.latitude = 28.5344643; starting.longitude = 77.2778694; CLLocationCoordinate2D final; final.latitude = 27.5344643; final.longitude = 76.2778694; – Manish Jain Feb 03 '11 at 14:36
  • i have use this and passed in the function but showing incompitable pointer error – Manish Jain Feb 03 '11 at 14:37
  • CLLocationCoordinate2D starting = CLLocationCoordinate2DMake(28.5344643, 77.2778694); i have also use this one but showing incompitable types in initalization – Manish Jain Feb 03 '11 at 14:42
  • but this distance is not accurate with google maps calculated distance – Manish Jain Feb 03 '11 at 14:55
  • this should be a double type on return and everywhere else, as location is a double. – Tom Andersen Nov 27 '12 at 17:39
  • The accuracy on this is limited by the fact that there is no altitude info - which is fine for locations near the same altitude, as long as they are not 100's of km apart. Even at a few hundred km, this is OK for most purposes. – Tom Andersen Nov 27 '12 at 18:44
  • You are after 3 decimal places of accuracy, then you can throw in an altitude fix see below. – Tom Andersen Nov 27 '12 at 18:59
2

This is an 'improvment' to the above solution. It adds in altitude information. It seems that the altitude that apple returns is in meters. Not suitable for flying or orbit or like that, but will work if someone is 15 floors directly above the other person, on a nearby mountain etc. Not extensively tested. It assumes that you don't care about altitude for something over 20km away. It then feeds in an altitude correction as you are closer to the other person. So for two people 20 metres away from each other, but 100m higher, you get a distance of about 102 meters. Right at the end I switch to km for the return. Also found a nan bug in the original code.

#define DEG2RAD(degrees) (degrees * 0.01745329251)
#define RADIUS_OF_EARTH 6371000.0
// km
+ (double)getDistanceFromStartCoords:(CLLocationCoordinate2D)start altStart:(double)altStart andEndCoords:(CLLocationCoordinate2D)end altEnd:(double)altEnd;
{
    double argument = (cos(DEG2RAD(start.latitude))*
                 cos(DEG2RAD(end.latitude))*
                 cos((-1*DEG2RAD(end.longitude))-
                     (-1*DEG2RAD(start.longitude)))) +
              (sin(DEG2RAD(start.latitude))*
               sin(DEG2RAD(end.latitude)));

    double dist = 0.0;
    if (argument < 1.0 && argument > -1.0) // acos will return nan for very small (0) distance
        dist = acos(argument)*RADIUS_OF_EARTH;
//    else
//        NSLog(@"found bug, %f", acos(argument));


    // Altitude hack.
    // blend in an altitude correction (blend for smoothness)
    // add in altitude difference
    double altDiff = fabs(altStart - altEnd); // altdiff
    double factor = 1.0 - dist/20000.0;
    if (factor < 0.0)
        factor = 0.0;

    dist += sqrt(dist*dist + factor*altDiff*altDiff);

    //NSLog(@"distance found, %f", dist);
    return dist/1000.0; // return km
}
Tom Andersen
  • 7,132
  • 3
  • 38
  • 55