[英]Android: how to get the walking distance between two geo coordinates?
我使用了这个查询网址
,在此问题的答案中给出。 但是在我尝试之后,它无法与坐标一起使用。 它与地址名称tho一起使用。 我想我可以使用Google的地理编码来首先获取地址。 但是我想知道是否还有另一种方法来获取两个坐标之间的步行距离?
应该可以向http://maps.google.com/maps/api/directions/<json|xml>?<params>
发出请求,并将坐标指定为origin
和destination
参数。
我短暂地尝试过,但是没有返回结果。
按照他们的文档,它应该可以工作,但是他们没有详细说明如何指定纬度和经度。
但是他们说这是可能的。
引用:
[...] origin(必填)-您希望从中计算路线的地址或 文本纬度/经度值 [...]
不过,这应该可以帮助您入门。
我建议使用JSON输出格式。
它解析起来要简单得多,并且应该占用更少的带宽(它比XML少一些冗长)。
它可以正常工作:这是一个示例网址: http://maps.google.com/maps/api/directions/json?origin=49.75332,6.50322&destination=49.71482,6.49944&mode=walking&sensor=false
: http://maps.google.com/maps/api/directions/json?origin=49.75332,6.50322&destination=49.71482,6.49944&mode=walking&sensor=false
可以使用Haversine公式轻松确定直线距离。 如果您从Google检索路线,则可以计算每个路段的距离并将其汇总。
前一段时间,我在博客文章( python和pl / 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
将其转换为Java应该很简单。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.