在Windows 8 Metro风格的应用程序中使用.NET GeoCoordinate.GetDistanceTo等效

Metro风格Windows 8应用程序中System.Device.Location.GeoCoordinate.GetDistanceTo方法的等效项是什么。

Metro应用程序具有Geocoordinate类(使用小写“C”)但没有GetDistanceTo方法。

其次,Metro Geocoordinate类没有构造函数。 我该如何创建它的实例。

由于某种原因,Metro Geocoordinate没有public构造函数。 在我看来,实现这个的最好方法是使用Reflector并复制System.Device.Location.GeoCoordinate的实现,它也会给你GetDistanceTo

希望以后可以将其重新添加到API中。

我根据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; } } 

我写了一个小的图书馆,用于一年前使用坐标,或者可以帮助你计算坐标之间的距离。 它可以在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);