在Windows 8 Metro风格应用中,与.NET GeoCoordinate.GetDistanceTo等效的内容是什么?

4

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

Metro 应用程序具有 Geocoordinate 类(小写字母 'C'),但没有 GetDistanceTo 方法。

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


1
请查看此链接:http://msdn.microsoft.com/zh-cn/library/windows/apps/windows.devices.geolocation.geocoordinate#Y0 - Rahul Tripathi
3个回答

26

根据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
请注意,结果以米为单位。 - Emond

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
是的,所以如果您需要在长距离上获得高精度,它并不是最好的选择。但在大多数情况下,它仍然可以胜任。 - Karl-Johan Sjögren
我正在考虑在.NET版本上使用反射器并编写一个扩展方法来实现这一点。 - Muhammad Rehan Saeed
1
另外一个解决方案,算法并不是很难,所以提取它应该相当容易。 - Karl-Johan Sjögren

2

Metro的Geocoordinate因某些原因没有public构造函数。我认为最好的方法是使用Reflector并复制System.Device.Location.GeoCoordinate的实现,这也将为您提供GetDistanceTo

希望这将在以后重新添加到API中。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接