Skip to content Skip to sidebar Skip to footer

How To Calculate The Distance Between Two Points In Android App

in my app i am trying to calculate the distance a person traveling from one place to the other. For that i am using the Haversine formula, R = earth’s radius (mean radius = 6,371

Solution 1:

This method is in standard API (Location.distanceBetween method)) http://developer.android.com/reference/android/location/Location.html

Solution 2:

Use the following formula to find the distance between two lat-longs:

privatedoublecalculateDistance(double lat1, double lon1, double lat2, double lon2, String unit){
      double theta = lon1 - lon2;
      double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
      dist = Math.acos(dist);
      dist = rad2deg(dist);
      dist = dist * 60 * 1.1515;
      if (unit == "K") {
        dist = dist * 1.609344;
      } elseif (unit == "M") {
        dist = dist * 0.8684;
        }
      return (dist);
}

    /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*//*::  This function converts decimal degrees to radians             :*//*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/privatedoubledeg2rad(double deg){
      return (deg * Math.PI / 180.0);
    }

    /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*//*::  This function converts radians to decimal degrees             :*//*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/privatedoublerad2deg(double rad){
      return (rad * 180.0 / Math.PI);
    }

Pass lat,longs with the function and unit in which you want distance ("K" for kilometer and "M" for Miles). Hope this will help you.

Solution 3:

Location.distanceBetween() will do this for two Latitude Longitude coordinate points.

Solution 4:

When you travel by road (Even via air-travel too), bearing of the Earth comes into play since the Earth is not flat. but this Haversine formula should already take care of it. Secondly, what people might have been saying, is that, when you travel via road, you don't go just straight between two points. you take quite a number of turns. And to "precisely" tell the distance between two points, you might want to take those turns into consideration too.

Solution 5:

i found the sample code in the following link to calculate the distance going through roads.

http://code.google.com/p/krvarma-android-samples/source/browse/#svn/trunk/GPSSample%253Fstate%253Dclosed

Post a Comment for "How To Calculate The Distance Between Two Points In Android App"