可以向http://maps.google.com/maps/api/directions/<json|xml>?<params>
发送请求,并将坐标指定为origin
和destination
参数。 我简要尝试了一下,但没有返回结果。根据他们的文档,它应该可以工作,但他们没有详细说明如何指定纬度和经度。但他们说这是可能的。引用:
[...] origin (required) — The address or textual latitude/longitude value from which you wish to calculate directions [...]
尽管如此,这应该能让您开始。我建议使用JSON输出格式。它更容易解析,而且应该使用更少的带宽(它比XML更简洁)。
它有效:这是一个示例URL:http://maps.google.com/maps/api/directions/json?origin=49.75332,6.50322&destination=49.71482,6.49944&mode=walking&sensor=false
可以使用Haversine公式轻松确定直线距离。如果从Google检索路线,则可以计算每个段的距离并将它们相加。
一段时间以前,我在博客文章中写下了(众所周知的)算法(Haversine)(python和pl/sql)
这是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应该很容易。