wil*_*ill 5 c# location windows-phone-7
我正在编写一个需要知道位置的Windows Phone 7应用程序.具体来说,我想要一些(c#)代码在手机进入特定位置(例如0.5英里)的(固定)范围内时运行.我有内存中物理位置的所有lat/long数据.我将使用Geo Coordinate Watcher类来获取设备的当前坐标.现在唯一的技巧是计算用户是否在任何位置的范围内.
谢谢!
更新:正如这里所承诺的那样,使用球面定律法计算距离的小C#函数.希望它可以帮助别人.注意:我正在编写Windows Phone 7应用程序,因此使用了GeoLocation类.如果你正在使用"常规"c#,那么你可以改变函数来接受函数需要的两个坐标对.
internal const double EarthsRadiusInKilometers = 6371;
/// <summary>
/// The simple spherical law of cosines formula
/// gives well-conditioned results down to
/// distances as small as around 1 metre.
/// </summary>
/// <returns>Distance between points "as the crow flies" in kilometers</returns>
/// <see cref="http://www.movable-type.co.uk/scripts/latlong.html"/>
private static double SpericalLawOfCosines(GeoCoordinate from, GeoCoordinate to)
{
return ( Math.Acos (
Math.Sin(from.Latitude) * Math.Sin(to.Latitude) +
Math.Cos(from.Latitude) * Math.Cos(to.Latitude) *
Math.Cos(to.Longitude - from.Longitude)
) * EarthsRadiusInKilometers)
.ToRadians();
}
/// <summary>
/// To a radian double
/// </summary>
public static double ToRadians(this double d)
{
return (Math.PI / 180) * d;
}
Run Code Online (Sandbox Code Playgroud)
既然您正在使用GeoCoordinate,那么为什么当它已经存在于该类中时自己实现呢?
var distance = coordinateA.GetDistanceTo(coordinateB);
Run Code Online (Sandbox Code Playgroud)
(其中coordinateA和B的类型为GeoCoordinate)
请参阅MDSN文档.