点和线之间的最短距离(Google 地图 API 问题?)

Shortest distance between a point and a line (Google Maps API issue?)

我正在尝试找出从 C 点到海滩的直线距离。海滩线由点 A 和 B 定义,使用 Haversine 公式我得到从 C(我在 Google 地图中的标记)到垂直于 C 的 AB 海滩线中的点 D 的距离。

一切正常,但 D 点不正确。我使用此代码查找 D:

function get_perp(C){
        var A = { lat:33.345678, lng:-117.518921 };
        var B = { lat:33.100678, lng:-117.318492 };

        t = ((C.lat-A.lat)*(B.lat-A.lat)+(C.lng-A.lng)*(B.lng-A.lng))/((B.lat-A.lat)*(B.lat-A.lat)+(B.lng-A.lng)*(B.lng-A.lng));

        var D = { lat:0,lng:0};
        D.lat = A.lat + t*(B.lat-A.lat);
        D.lng = A.lng + t*(B.lng-A.lng);

        return D;
}

返回的D点确实是直线上的一个点,但不垂直于C。是当AB线水平或垂直时,但不是时AB与CD的夹角不对。

我尝试了在这里找到的其他函数,但所有函数都导致相同的结果。

在这个fiddle中是整个过程,如果你放大足够大,你可以看到AB和CD线不垂直:Shortest distance from AB to C

编辑:在 geogebra 中使用它,我可以看到该功能在找到点方面是可行的。当 google maps api 表示该点时,就会发生错误。 Geogebra

您使用平面几何 方法进行计算,但对于球面几何,它们是错误的。 (C.f.:请注意,您使用 Haversine 公式而不是 Pythagorean 公式找到距离)。

this page你可以找到算法和JS代码来计算交叉轨道距离和沿轨道距离(这可能用于使用从第一点开始的方位和这个距离来找到D点)

Cross-track distance
Here’s a new one: I’ve sometimes been asked about distance of a
point from a great-circle path (sometimes called cross track
error).

Formula:    dxt = asin( sin(δ13) ⋅ sin(θ13−θ12) ) ⋅ R
where   δ13 is (angular) distance from start point to third point
θ13 is (initial) bearing from start point to third point
θ12 is (initial) bearing from start point to end point
R is the earth’s radius
JavaScript: 
var δ13 = d13 / R;
var dXt = Math.asin(Math.sin(δ13)*Math.sin(θ13-θ12)) * R;
Here, the great-circle path is identified by a start point and 
an end point – depending on what initial data you’re working from,
you can use the formulæ above to obtain the relevant distance 
and bearings. The sign of dxt tells you which side of the path
the third point is on.

The along-track distance, from the start point to the closest 
point on the path to the third point, is

Formula:    dat = acos( cos(δ13) / cos(δxt) ) ⋅ R
where   δ13 is (angular) distance from start point to third point
δxt is (angular) cross-track distance
R is the earth’s radius
JavaScript: 
var δ13 = d13 / R;
var dAt = Math.acos(Math.cos(δ13)/Math.cos(dXt/R)) * R;

据我所知,你的 D 公式是正确的,并且线性近似在如此小的范围内是合理的(增量大约四分之一度;由于非线性引起的相对误差应该在10^-5 的顺序)。

您看到的可能是由于地图投影不一致(不保留角度),导致角度显示不正确。但观点是正确的。

你知道他们用的是哪种投影吗?


Bingo,角度刚刚好,只是投影的显示效果。