distanceBetween() returns inaccurate result?

Marcin S. picture Marcin S. · Jan 31, 2013 · Viewed 16.9k times · Source

I use distanceBetween() of Location class to calculate the distance between two points as follows:

private float getDistanceInMiles(GeoPoint p1, GeoPoint p2) {

    double lat1 = ((double)p1.getLatitudeE6()) / 1e6;
    double lng1 = ((double)p1.getLongitudeE6()) / 1e6;
    double lat2 = ((double)p2.getLatitudeE6()) / 1e6;
    double lng2 = ((double)p2.getLongitudeE6()) / 1e6;
    float [] dist = new float[1];
    Log.i("destination coordinates", "Latitude:" + lat2 + ", Longitude: " + lng2);
        Location.distanceBetween(lat1, lng1, lat2, lng2, dist);

    return dist[0] * 0.000621371192f;
}

Documentation says that distanceBetween() "Computes the approximate distance in meters between two locations, and optionally the initial and final bearings of the shortest path between them." However the difference between the result returned by distanceBetween() and real GPS device or Google Navigation app is pretty big. For example my method will return 6.2 miles while google maps shows 10 miles for the same location. I double check the coordinates of the starting point p1 and ending point p2 and they seems to be correct. Is that how distanceBetween() method works or Am I doing something wrong? And by the way Is there a way to use Google Place API to retrieve a distance as a JSON response?

Distance calculated by Google Maps: 6.1 miles

enter image description here

And the result of

Location.distanceBetween(41.742964, -87.995971, 41.811511, -87.967923, dist) is 

4.947700

Answer

CommonsWare picture CommonsWare · Jan 31, 2013

Google Navigation generally reports driving or walking distance along the set of steps in the directions list, not straight-line distance, which is what distanceBetween() reports.