我试图找到从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之间的角度不正确时。
我尝试了在这里找到的其他功能,但是所有这些都会导致相同的结果。
在这个小提琴中,这是整个过程,如果缩放得足够大,您会看到AB和CD线不垂直:Shortest distance from AB to C
编辑:在geogebra中玩它我可以看到找到点的功能正常。然后,当Google Maps api代表该点时,就会发生错误。 Geogebra
答案 0 :(得分:1)
您使用平面几何方法进行计算,但对于球形几何是错误的。 (C.f .:请注意,您是使用Haversine公式而不是毕达哥拉斯公式找到距离的。)
在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;
答案 1 :(得分:1)