![](/img/trans.png)
[英]Find a point after specific distance in straight line between 2 Points in google maps API
[英]Shortest distance between a point and a line (Google Maps API issue?)
我試圖找到從C點到海灘的直線距離。 沙灘線由A點和B點定義,使用Haversine公式,我得到從C(我在Google地圖中的標記)到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線不垂直: AB到C的最短距離
編輯:在geogebra中玩它我可以看到該功能可以找到要點。 然后,當Google Maps api代表該點時,就會發生錯誤。 代數
您可以使用平面幾何方法進行計算,但對於球形幾何卻不正確。 (cf:請注意,您使用Haversine公式而不是畢達哥拉斯公式找到了距離)。
在此頁面上,您可以找到算法和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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.