2012-09-16 61 views
4

Metro風格Windows 8應用程序中System.Device.Location.GeoCoordinate.GetDistanceTo方法的等價物是什麼。Windows 8中的.NET GeoCoordinate.GetDistanceTo Equivelant Metro風格Apps

Metro應用程序具有Geocoordinate類(使用小寫'C'),但沒有GetDistanceTo方法。

其次,Metro Geocoordinate類沒有構造函數。我如何創建它的一個實例。

+1

看看這個鏈接。 http://msdn.microsoft.com/en-us/library/windows/apps/windows.devices.geolocation.geocoordinate#Y0 –

回答

2

地鐵Geocoordinate沒有public構造出於某種原因。在我看來,實現這一點的最好方法是使用反射器並複製System.Device.Location.GeoCoordinate的實現,這也將給你GetDistanceTo

希望這將在以後重新添加到API。

2

我寫了一個小庫來處理一年前的座標或者可以幫助你計算座標之間距離的東西。它可以在CodePlex上獲得:http://beavergeodesy.codeplex.com/

然後計算距離就像這樣簡單。

// Create a pair of coordinates 
GeodeticCoordinate coordinate1 = new GeodeticCoordinate() { Latitude = 63.83451d, Longitude = 20.24655d }; 
GeodeticCoordinate coordinate2 = new GeodeticCoordinate() { Latitude = 63.85763d, Longitude = 20.33569d }; 
// Calculate the distance between the coordinates using the haversine formula 
double distance = DistanceCalculator.Haversine(coordinate1, coordinate2); 
+1

這個公式不是假定地球是球形的嗎? – CodesInChaos

+1

是的,所以如果您需要長距離的高精度,這並不是最好的。在大多數情況下,它會做得很好,但。 –

+0

我正在考慮在.NET版本上使用反射器並編寫擴展方法來執行此操作。 –

23

我根據BlackLight反編譯.NET 4.5 GetDistanceTo方法。在這裏複製以節省人力。

public double GetDistanceTo(GeoCoordinate other) 
{ 
    if (double.IsNaN(this.Latitude) || double.IsNaN(this.Longitude) || double.IsNaN(other.Latitude) || double.IsNaN(other.Longitude)) 
    { 
     throw new ArgumentException(SR.GetString("Argument_LatitudeOrLongitudeIsNotANumber")); 
    } 
    else 
    { 
     double latitude = this.Latitude * 0.0174532925199433; 
     double longitude = this.Longitude * 0.0174532925199433; 
     double num = other.Latitude * 0.0174532925199433; 
     double longitude1 = other.Longitude * 0.0174532925199433; 
     double num1 = longitude1 - longitude; 
     double num2 = num - latitude; 
     double num3 = Math.Pow(Math.Sin(num2/2), 2) + Math.Cos(latitude) * Math.Cos(num) * Math.Pow(Math.Sin(num1/2), 2); 
     double num4 = 2 * Math.Atan2(Math.Sqrt(num3), Math.Sqrt(1 - num3)); 
     double num5 = 6376500 * num4; 
     return num5; 
    } 
} 
+1

請注意,結果是以米爲單位 –

相關問題