【问题标题】:How to get straight distance between two location in android?如何在android中获得两个位置之间的直线距离?
【发布时间】:2013-07-11 13:38:14
【问题描述】:

首先仔细阅读问题...

我需要直线距离不是步行、开车

看看下面给出的这张图片,

Google 为我们提供驾车和驾车距离。

但我不想要,我想要两个位置之间的直线距离(纬度 - 经度)。

显示为 RED LINE。

注意:我不想在谷歌地图上画红线,只想要距离单位(英里、公里等)

【问题讨论】:

  • 111.325 公里 =(近)1 度
  • 这并不总是真正的塞尔文。经度因纬度而异。
  • 这是一个练习:下次让我们在提问时尽量保持礼貌。

标签: android distance


【解决方案1】:

安卓

double distance

Location locationA = new Location(“point A”)

locationA.setLatitude(latA);

locationA.setLongitude(lngA);

Location locationB = new Location(“point B”);

locationB.setLatitude(latB);

LocationB.setLongitude(lngB);

distance = locationA.distanceTo(locationB);

数学

a = distance in degrees //meterConversion = 1609;

b = 90 - latitude of point 1

c = 90 - latitude of point 2

l = longitude of point 1 - longitude of point 2

Cos(a) = Cos(b)Cos(c) + Sin(b)Sin(c)Sin(l)

d = circumference of Earth * a / 360 // circumference of Earth = 3958.7558657440545D km

【讨论】:

    【解决方案2】:

    Haversine function 用于查找球体上两点之间的距离。

    将其扩展到寻找地球上两点之间的直线距离是相当简单的。地球不是一个完美的球体,但这仍然是使用标准测量(称为 WGS84)赤道半径的一个很好的近似值。

    正如 CommonsWare 所说,您可以非常简单地使用 distanceBetween(),它使用 Haversine 函数和 WGS84 半径。

    为了更好地理解实现/数学,请查看 Python 中的 sample code

    【讨论】:

      【解决方案3】:

      您使用以下代码找到的距离。 你只需要得到两个geoPoint的纬度和经度。 并在下面的计算中使用它来获得距离。

       R = 6371; // km
       d = Math.acos(Math.sin(lat1)*Math.sin(lat2) + 
                    Math.cos(lat1)*Math.cos(lat2) *
                    Math.cos(lon2-lon1)) * R;
      

      这将是所有计算后的返回距离。

      R是KM的表面半径,需要在计算中使用,你试试这个。我希望它对你有用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-08-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多