0

i have used Haversine for calculating distance between two location.

public static class Haversine {

    static int Radius = 6371;
    public static double haversine(double lat1, double lon1, double lat2,
            double lon2) {

        double dLat = Math.toRadians(lat2 - lat1);
        double dLon = Math.toRadians(lon2 - lon1);
        lat1 = Math.toRadians(lat1);
        lat2 = Math.toRadians(lat2);

         double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
                    + Math.cos(Math.toRadians(lat1))
                    * Math.cos(Math.toRadians(lat2)) * Math.sin(dLon / 2)
                    * Math.sin(dLon / 2);
            double c = 2 * Math.asin(Math.sqrt(a));
            double valueResult = Radius * c;
            double km = valueResult / 1;
            DecimalFormat newFormat = new DecimalFormat("####");
            int kmInDec = Integer.valueOf(newFormat.format(km));
            double meter = valueResult % 1000;
            int meterInDec = Integer.valueOf(newFormat.format(meter));
            Log.i("Radius Value", "" + valueResult + "   KM  " + kmInDec
                    + " Meter   " + meterInDec);

            return Radius * c;

        /*double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1)* Math.cos(lat2);
        double c = 2 * Math.asin(Math.sqrt(a));

        return R * 2 * Math.asin(Math.sqrt(a));*/
    }



}

From the above code i am not able to get exact distance between 2 location.

When i run the above mehtod then it shows 4.32 km from my two places but when i checked on the google map then it shows the 5.3 km .

i have also used Location.distanceBetween method still it shows the 4.32 km .

How can i get exact distance between location?

  • Have a look at http://stackoverflow.com/questions/1502590/calculate-distance-between-two-points-in-google-maps-v3 – Balwinder Singh Nov 04 '15 at 05:47
  • I have seen that . implement on that basis. –  Nov 04 '15 at 05:49
  • I guess the above formula will calculate displacement not distance. You can use Google Maps Distance Matrix Api. https://developers.google.com/maps/documentation/distance-matrix/intro – Muhammad Babar Nov 04 '15 at 06:06

2 Answers2

0

You can see this link. Haversine and Location.distanceBetween method are both the origin to the point at which a line.
So, you can use http://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&sensor=false to get the real distance.

Community
  • 1
  • 1
WhiteBanana
  • 349
  • 4
  • 20
  • Oh... so `Haversine` is not accurate for this reason. but how can i use this in `android` ? –  Nov 04 '15 at 06:16
  • This isn't the right way to get distance depends googles latest docs.this won't work for you all the time.. https://developers.google.com/maps/documentation/distance-matrix/intro – Anoop M Maddasseri Nov 04 '15 at 06:39
0

From Google official repository link

SphericalUtil

MathUtil

Usage

 double distance = SphericalUtil.computeDistanceBetween(new LatLng(9.000,10.00), new LatLng(9.000,11.00));

The above method will returns the distance between two LatLngs, in meters. Or try this

private String getDistance(LatLng my_latlong,LatLng frnd_latlong){
 Location l1=new Location("One");
 l1.setLatitude(my_latlong.latitude);
 l1.setLongitude(my_latlong.longitude);

 Location l2=new Location("Two");
 l2.setLatitude(frnd_latlong.latitude);
 l2.setLongitude(frnd_latlong.longitude);

 float distance=l1.distanceTo(l2);
 String dist=distance+" M";

  if(distance>1000.0f)
   {
     distance=distance/1000.0f;
     dist=distance+" KM";
   }

 return dist;

}

or you can give a look at link

Anoop M Maddasseri
  • 10,213
  • 3
  • 52
  • 73