Android:如何获得两个地理协调之间的步行距离?

wei*_*wei 5 java gis google-maps geolocation geospatial

我使用了这个查询网址

http://maps.google.com/maps?q=from+A+to+B&output=kml

,这是在这个问题的答案中给出的.但在我尝试之后,它不适用于坐标.它适用于地址名称.我想我可以使用谷歌的地理编码来获取地址.但我想知道是否还有另一种方法来获得两个坐标之间的步行距离?

exh*_*uma 5

我的新答案:)

使用Google Directions API.

应该可以请求http://maps.google.com/maps/api/directions/<json|xml>?<params>和指定coords origindestination参数.我简单地试了一下,但没有结果.沿着他们的文档它应该工作,但他们没有详细解释如何指定纬度和经度.但他们说这是可能的.引用:

[...]原产地(必填) - 您希望从中计算方向的地址或文本纬度/经度值 [...]

不过,这应该让你开始.我建议使用JSON输出格式.解析起来要简单得多,并且应该消耗更少的带宽(它不像XML那么冗长).

它的工作原理:这是一个示例网址:http://maps.google.com/maps/api/directions/json?origin=49.75332,6.50322&destination=49.71482,6.49944&mode=walking&sensor=false

我以前的答案

使用Haversine公式可以轻松确定直线距离.如果您从Google检索路线,那么您可以计算每个细分的距离并将它们相加.

不久前,我在博客文章(pythonpl/sql)中写下了(众所周知的)算法(Haversine)

这是python代码的副本:

from math import sin, cos, radians, sqrt, atan2

    def lldistance(a, b):
   """
   Calculates the distance between two GPS points (decimal)
   @param a: 2-tuple of point A
   @param b: 2-tuple of point B
   @return: distance in m
   """
   r = 6367442.5             # average earth radius in m
   dLat = radians(a[0]-b[0])
   dLon = radians(a[1]-b[1])
   x = sin(dLat/2) ** 2 + \
       cos(radians(a[0])) * cos(radians(b[0])) *\
       sin(dLon/2) ** 2
   #original# y = 2 * atan2(sqrt(x), sqrt(1-x))
   y = 2 * asin(sqrt(x))
   d = r * y

   return d
Run Code Online (Sandbox Code Playgroud)

将其翻译成Java应该是微不足道的.