how to calculate the distance between two points in android app

社会主义新天地 提交于 2019-12-04 04:18:52

问题


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,371km)
Δlat = lat2− lat1
Δlong = long2− long1
a = sin²(Δlat/2) + cos(lat1).cos(lat2).sin²(Δlong/2)
c = 2.atan2(√a, √(1−a))
d = R.c 

getting the latitude and longitude of starting place and reaching place i am calculating the distance in kms. But others say that this distance calculation works only if travelled by airways and get varies if user travels by roadways.

If it is so how can i get a correct distance while traveling through road ways.

please help me friends


回答1:


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




回答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.




回答3:


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.




回答4:


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




回答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




回答6:


Its seems distance trough radways bigger, because trains and trucks moves not along straight line but airplances do.

You need road map to occurate compute distance



来源:https://stackoverflow.com/questions/5739734/how-to-calculate-the-distance-between-two-points-in-android-app

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!