-1

I have used locationA.distanceTo(locationB); and also have copy pasted a method written for haversine formula.

The actual distance between the two geopoints is 25m but i get a result of 6894.52192658389 using the above methods.

Followed this:Creating a method using Haversine Formula, Android V2 and this Find distance between two points on map using Google Map API V2

Checked distance using this free tool http://www.onlineconversion.com/map_greatcircle_distance.htm

Community
  • 1
  • 1
nishon.tan
  • 969
  • 2
  • 8
  • 21

1 Answers1

0

You can try the code below :

public static float distFrom(float lat1, float lng1, float lat2, float lng2) {
    double earthRadius = 6371000; //meters
    double dLat = Math.toRadians(lat2-lat1);
    double dLng = Math.toRadians(lng2-lng1);
    double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
               Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
               Math.sin(dLng/2) * Math.sin(dLng/2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
    float dist = (float) (earthRadius * c);

    return dist;
    }

It will gives you the result in meters, you can convert to any required unit.

Umais Gillani
  • 608
  • 4
  • 9