[英]Geographic Midpoint between two coordinates
我一直在使用Moveable-Type網站來幫助我進行一些Geocoordinate計算並且它非常有用,但是,我在計算兩個坐標之間的中點時遇到了一個錯誤。 我的結果接近預期,但不夠接近:
posA = {47.64570362, -122.14073746}
posB = {47.64316917, -122.14032175}
預期結果(取自可移動型計算器)= 47°38'40“N,122°08'26”W = {47.644444, -122.140556}
我的結果: {49.6054801645915, -122.14052959995759}
這是我的代碼:
private Geocoordinate MidPoint(Geocoordinate posA, Geocoordinate posB)
{
Geocoordinate midPoint = new Geocoordinate();
double dLon = DegreesToRadians(posB.Longitude - posA.Longitude);
double Bx = Math.Cos(DegreesToRadians(posB.Latitude)) * Math.Cos(dLon);
double By = Math.Cos(DegreesToRadians(posB.Latitude)) * Math.Sin(dLon);
midPoint.Latitude = RadiansToDegrees(Math.Atan2(Math.Sin(DegreesToRadians(posA.Latitude)) + Math.Sin(DegreesToRadians(posB.Latitude)),
Math.Sqrt((Math.Cos(DegreesToRadians(posA.Latitude)) + Bx) * (Math.Cos(DegreesToRadians(posA.Latitude))) + Bx) + By * By));
midPoint.Longitude = posA.Longitude + RadiansToDegrees(Math.Atan2(By, Math.Cos(DegreesToRadians(posA.Latitude)) + Bx));
return midPoint;
}
我有幾種私有方法可以在Degrees和Radians之間進行轉換。 例如
private double DegreeToRadian(double angle)
{
return Math.PI * angle / 180.0;
}
我無法弄清楚為什么我的結果在Lat值上偏離了幾度。 有任何想法嗎?
謝謝
你把一些括號弄錯了。 我在代碼中標出了這個位置。
private Geocoordinate MidPoint(Geocoordinate posA, Geocoordinate posB)
{
Geocoordinate midPoint = new Geocoordinate();
double dLon = DegreesToRadians(posB.Longitude - posA.Longitude);
double Bx = Math.Cos(DegreesToRadians(posB.Latitude)) * Math.Cos(dLon);
double By = Math.Cos(DegreesToRadians(posB.Latitude)) * Math.Sin(dLon);
midPoint.Latitude = RadiansToDegrees(Math.Atan2(
Math.Sin(DegreesToRadians(posA.Latitude)) + Math.Sin(DegreesToRadians(posB.Latitude)),
Math.Sqrt(
(Math.Cos(DegreesToRadians(posA.Latitude)) + Bx) *
(Math.Cos(DegreesToRadians(posA.Latitude)) + Bx) + By * By)));
// (Math.Cos(DegreesToRadians(posA.Latitude))) + Bx) + By * By)); // Your Code
midPoint.Longitude = posA.Longitude + RadiansToDegrees(Math.Atan2(By, Math.Cos(DegreesToRadians(posA.Latitude)) + Bx));
return midPoint;
}
一些方法(例如WGS84慣例)包括地球的扁率。 ( 至少這就是它在這里所說的 )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.