计算 GPS 坐标和路线之间的最短距离

Calculate shortest distance between a GPS coordinate and a route

有没有一种简单的方法可以使用 Google 地图 API 计算路线(例如两个城市之间的方向)和 GPS 坐标之间的最近距离?

我能想到的一种方法是将路线转换为一组 GPS 坐标并计算到每个坐标的距离。有更好的方法吗?

One appoach I can think of is to translate the route into a set of GPS coordinates and calculate the distance to each coordinate. Is there a better way of doing this?

是的,有更好的方法。
第一部分是正确的:转换为 lat/lon 坐标的折线。 然而,第二个是简单的。您想要的是到线段的最短距离,而不是下一个角点,即线的起点或终点。

在学校你用的是 hessian 法线距离。但是,此公式仅计算到无限直线的距离。如果是多段线,您有一系列线段。

所以您需要一个公式 "distance to line segment"。 您可以使用该搜索词找到这个公式。

实施此公式后,您对所有由两个点 (polyPoint[i], polyPoint[i+1]) 定义的线段进行交互,并取距离的最小值。

不要忘记将当前线段转换为笛卡尔 (x,y) space,因为通常的公式不适用于球坐标。

这种方法比较复杂,但给出了准确的结果。如果通常长 400-700 米,请记住路线上的一条线。所以你问的简单方法,如果你在两点之间的中间,在这种情况下会给出 200-350m 的错误。