c# - 如何在 Windows Phone 8.1 中获取两个位置之间的距离

标签 c# windows-phone-8 windows-phone-8.1 .net-4.5

在 Windows Phone 8 中似乎有方法

   GeoCoordinate.GetDistanceTo()

用于计算两个位置之间的距离。 (即使未找到该方法的 the reference page。)

但是在整个 Windows Phone 8.1 Geolocation namespace 中等价的地方在哪里? ?

我根本找不到计算两个位置之间距离的方法。

WP8.1如何计算两个位置之间的距离?

最佳答案

GeoCoordinate.GetDistanceTo() 位于 System.Device.Location 命名空间中。但是 Windows 8.1(运行时应用程序)应用程序使用 Windows.Devices.Geolocation 命名空间,其中 GetDistanceTo() 方法不存在。

因此您可以使用Haversine 公式 自行计算距离。这是 wikipedia Haversine page , 你可以从那里知道公式。

您可以使用下面的 C# 代码,它使用 Haversine 公式来计算两个坐标之间的距离。

using System;  
namespace HaversineFormula  
{  
/// <summary>  
/// The distance type to return the results in.  
/// </summary>  
public enum DistanceType { Miles, Kilometers };  
/// <summary>  
/// Specifies a Latitude / Longitude point.  
/// </summary>  
public struct Position  
{  
    public double Latitude;  
    public double Longitude;  
}  
class Haversine  
{  
    /// <summary>  
    /// Returns the distance in miles or kilometers of any two  
    /// latitude / longitude points.  
    /// </summary>  
    public double Distance(Position pos1, Position pos2, DistanceType type)  
    {  
        double R = (type == DistanceType.Miles) ? 3960 : 6371;  
        double dLat = this.toRadian(pos2.Latitude - pos1.Latitude);  
        double dLon = this.toRadian(pos2.Longitude - pos1.Longitude);  
        double a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) +  
            Math.Cos(this.toRadian(pos1.Latitude)) * Math.Cos(this.toRadian(pos2.Latitude)) *  
            Math.Sin(dLon / 2) * Math.Sin(dLon / 2);  
        double c = 2 * Math.Asin(Math.Min(1, Math.Sqrt(a)));  
        double d = R * c;  
        return d;  
    }  
    /// <summary>  
    /// Convert to Radians.  
    /// </summary>  
    private double toRadian(double val)  
    {  
        return (Math.PI / 180) * val;  
    }  
}

关于c# - 如何在 Windows Phone 8.1 中获取两个位置之间的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28569246/

相关文章:

c# - 路由和消息处理程序 : request processing order issue

c# - 在 NLog 中使用依赖注入(inject)

c# - Windows Phone 8.1 中的警报通知

windows - Windows Phone 8 phonegap CLI 中的 msbuild 失败

c# - 如何在 Windows Phone 8 中实现深度链接

c# - 获取 MusicLibrary 时 Windows Phone 8.1 应用商店应用程序出错(调用 COM 组件已返回错误 HRESULT E_FAIL)

c# - 如何获取当前页面的类型并在 Windows Phone 8 中刷新它

drop-down-menu - Windows Phone 8.1 下拉列表

c# - 通过 C# 获取和设置初始化 : Jeffrey Richter, CLR 中的误解

c# - WPF ListBoxItem IsMouseOver