2010-11-12 101 views
6

我一直在使用可移動類型網站來幫助我進行一些地理座標計算,但它非常有用,但是,我在計算兩個座標之間的中點時出現了一個錯誤。我的結果是接近預期,但不足夠接近:兩個座標之間的地理中點

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; 
} 

我有一對夫婦的私有方法做角度和弧度和背部之間的轉換。 例如

private double DegreeToRadian(double angle) 
{ 
    return Math.PI * angle/180.0; 
} 

我不能算出爲什麼我的結果關於緯度值的幾度關閉。有任何想法嗎?

謝謝

+0

關我**整**度(你期望'34.8954',但得到'29.8954')或**近似**(你期望'34.8954',但得到'29.7836')? – Brad 2010-11-12 13:20:07

+0

我不太確定你是否將地球表面看作一個簡單的球體或橢球體。我最後一次使用蘭伯特定律[使用Java實現]工作,它似乎準確工作。 – anirvan 2010-11-12 13:26:17

+0

謝謝。它在經度上出了2度,而且lon很顯眼。我在本網站使用中點計算公式,翻譯成C#http://www.movable-type.co.uk/scripts/latlong.html – Stevieboy84 2010-11-12 14:36:35

回答

7

你放錯了一些括號。我在代碼中標記了位置。

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; 
} 
+0

:(我正要發佈這個,很好的接收。 – Jere 2010-11-12 18:21:30

+0

謝謝!!!我以爲這會很簡單,我不知道我是怎麼錯過的!你是一個明星! – Stevieboy84 2010-11-13 21:48:21

+0

我很高興我有幫助 :) – PetPaulsen 2010-11-13 22:19:35

相關問題