我想找到从C点到海滩的直线距离。海滩线由A点和B点定义,并且使用Haversine公式我得到C(我在谷歌地图中的标记)到AB垂直于C.海滩线的点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之间的角度不对时。
我尝试过我在这里找到的另一个函数但是所有这些函数都会导致相同的结果。
在这个小提琴中它是整个过程,如果你放大足够你可以看到AB和CD线不垂直:Shortest distance from AB to C
编辑:在地理代数中玩它我可以看到该功能可以找到点。当谷歌地图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;