【问题标题】:Equivelant of .NET GeoCoordinate.GetDistanceTo in Windows 8 Metro style Apps等效于 Windows 8 Metro 风格应用中的 .NET GeoCoordinate.GetDistanceTo
【发布时间】:2012-09-08 22:15:47
【问题描述】:

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

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

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

【问题讨论】:

标签: c# .net windows-8 microsoft-metro getdistance


【解决方案1】:

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

【讨论】:

  • 注意结果以米为单位
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    一年前我写了一个小库来处理坐标或者可以帮助你计算坐标之间距离的东西。它在 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);
    

    【讨论】:

    • 这个公式不是假设地球是球形的吗?
    • 是的,所以如果您需要长距离的高精度,这并不是最好的选择。在大多数情况下,它会做得很好。
    • 我正在考虑在 .NET 版本上使用反射器并编写一个扩展方法来做到这一点。
    • 也是一个解决方案,算法真的没有那么难,所以应该很容易提取出来。
    猜你喜欢
    • 2012-07-05
    • 1970-01-01
    • 1970-01-01
    • 2011-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-17
    • 1970-01-01
    相关资源
    最近更新 更多