问题描述:

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?

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;
}
```

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.

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
}
```