C# 如何在Windows Phone 8.1中获取两个位置之间的距离
在WindowsPhone8中似乎有一种方法C# 如何在Windows Phone 8.1中获取两个位置之间的距离,c#,windows-phone-8,windows-phone-8.1,.net-4.5,C#,Windows Phone 8,Windows Phone 8.1,.net 4.5,在WindowsPhone8中似乎有一种方法 GeoCoordinate.GetDistanceTo() 用于计算两个位置之间的距离。(即使找不到该方法的。) 但是,整体上的等价物在哪里呢 我根本找不到计算两个位置之间距离的方法 如何计算WP8.1中两个位置之间的距离?GeoCoordination.GetDistanceTo()位于System.Device.Location命名空间中。但windows 8.1(运行时应用程序)应用程序使用windows.Devices.Geoloca
GeoCoordinate.GetDistanceTo()
用于计算两个位置之间的距离。(即使找不到该方法的。)
但是,整体上的等价物在哪里呢
我根本找不到计算两个位置之间距离的方法
如何计算WP8.1中两个位置之间的距离?
GeoCoordination.GetDistanceTo()
位于System.Device.Location
命名空间中。但windows 8.1(运行时应用程序)应用程序使用windows.Devices.Geolocation
命名空间,其中GetDistanceTo()
方法不存在
因此,您可以使用哈弗公式自己计算距离。这是公式,你可以从那里知道公式
您可以使用下面的C代码,该代码使用哈弗森公式计算两个坐标之间的距离
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;
}
}
使用系统;
名称空间HaversineFormula
{
///
///返回结果的距离类型。
///
公共枚举距离类型{英里,公里};
///
///指定纬度/经度点。
///
公共结构位置
{
公共双纬度;
公共双经度;
}
哈弗森类
{
///
///返回任意两个的距离(以英里或公里为单位)
///纬度/经度点。
///
公共双距离(位置1、位置2、距离类型)
{
双R=(类型==距离类型.Miles)?3960:6371;
双dLat=这个.托拉第安(位置2.纬度-位置1.纬度);
double dLon=this.toRadian(pos2.Longitude-pos1.Longitude);
双a=Math.Sin(dLat/2)*Math.Sin(dLat/2)+
Math.Cos(this.toRadian(pos1.Latitude))*Math.Cos(this.toRadian(pos2.Latitude))*
数学单(dLon/2)*数学单(dLon/2);
double c=2*Math.Asin(Math.Min(1,Math.Sqrt(a));
双d=R*c;
返回d;
}
///
///转换成弧度。
///
私人双托拉迪安(双瓦尔)
{
返回值(Math.PI/180)*val;
}
}