1个回答

5

我的新答案 :)

使用Google Directions API

可以向http://maps.google.com/maps/api/directions/<json|xml>?<params>发送请求,并将坐标指定为origindestination参数。 我简要尝试了一下,但没有返回结果。根据他们的文档,它应该可以工作,但他们没有详细说明如何指定纬度和经度。但他们说这是可能的。引用:

[...] 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)(pythonpl/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应该很容易。


谢谢!但我特别关心步行距离。现在的问题是,我不知道如何仅通过坐标从谷歌获取路线。我尝试在浏览器中使用带有坐标的URL,但它说无法理解该位置。 - wei
我不知道答案中的编辑是否会触发通知...所以我会另外写这个评论来处理它。 - exhuma
非常感谢!经纬度的格式是什么?十进制的吗?我用我的位置试了一下,但没有结果。 - wei
不用管了,现在它可以工作了!是纬度,经度而不是经度,纬度。感谢你的帮助! - wei

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接