How To Calculate The Distance Between Two Points In Android App
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:
private double calculateDistance(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;
} else if (unit == "M") {
dist = dist * 0.8684;
}
return (dist);
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*:: This function converts decimal degrees to radians :*/
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
private double deg2rad(double deg)
{
return (deg * Math.PI / 180.0);
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*:: This function converts radians to decimal degrees :*/
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
private double rad2deg(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.
Post a Comment for "How To Calculate The Distance Between Two Points In Android App"