如何计算Android应用程序中两点之间的距离

发布于 2024-11-02 18:45:59 字数 384 浏览 1 评论 0原文

在我的应用程序中,我试图计算一个人从一个地方到另一个地方的旅行距离。为此,我使用半正矢公式,

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 

获取起始地点和到达地点的纬度和经度,我正在计算以公里为单位的距离。但其他人表示,这种距离计算仅在乘坐飞机出行时有效,如果用户乘坐公路出行,则距离计算会有所不同。

如果是这样,我在道路上行驶时如何获得正确的距离。

请帮助我朋友

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

孤单情人 2024-11-09 18:45:59

此方法在标准 API 中(Location.distanceBetween 方法))
http://developer.android.com/reference/android/location/Location.html

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

卷耳 2024-11-09 18:45:59

使用以下公式计算两个经纬度之间的距离:

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);
    }

将纬度、经度与所需距离的函数和单位(“K”表示公里,“M”表示英里)传递。
希望这会对您有所帮助。

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.

兮颜 2024-11-09 18:45:59

Location.distanceBetween() 将为两个纬度经度坐标点执行此操作。

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

戒ㄋ 2024-11-09 18:45:59

当您通过公路旅行(甚至也通过航空旅行)时,地球的方位角就会发挥作用,因为地球并不平坦。但这个半正矢公式应该已经可以解决这个问题了。其次,人们可能会说,当您通过公路旅行时,您不会直接在两点之间行驶。你要转很多圈。为了“精确”地告诉两点之间的距离,您可能还需要考虑这些转弯。

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.

恏ㄋ傷疤忘ㄋ疼 2024-11-09 18:45:59

我在以下链接中找到了示例代码来计算通过道路的距离。

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

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

我也只是我 2024-11-09 18:45:59

它的距离似乎更大,因为火车和卡车不是沿直线移动,但飞机却沿直线移动。

您需要路线图来计算距离

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文