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

hur*_*ppi 2 c# .net-4.5 windows-phone-8 windows-phone-8.1

在Windows Phone 8中似乎有方法

   GeoCoordinate.GetDistanceTo()
Run Code Online (Sandbox Code Playgroud)

用于计算两个位置之间的距离.(即使找不到该方法的参考页面.)

但是整个Windows Phone 8.1 Geolocation命名空间中的等效位置在哪里?

我找不到一种方法来计算两个位置之间的距离.

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

Uts*_*awn 8

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

因此,您可以使用Haversine公式自行计算距离.这是维基百科的Haversine页面,你可以从那里了解公式.

您可以使用下面的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;  
    }  
}
Run Code Online (Sandbox Code Playgroud)