我正在编写一个需要位置感知的 Windows Phone 7 应用程序。具体来说,我希望当手机进入特定位置的(固定)范围(例如 0.5 英里)时运行一些(c#)代码。我拥有内存中物理位置的所有纬度/经度数据。我将使用地理坐标观察者类 http://msdn.microsoft.com/en-us/library/system.device.location.geocoordinatewatcher.aspx获取设备当前坐标。现在唯一的技巧是计算用户是否在任何位置的范围内。
Thanks!
Update:正如所承诺的,这是一个使用 C# 的小函数余弦球面定律 http://www.movable-type.co.uk/scripts/latlong.html计算距离的方法。希望它可以帮助别人。注意:我正在编写一个 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;
}
既然您正在使用 GeoCoordinate,那么当它已经存在于该类中时为什么要自己实现它呢?
var distance = coordinateA.GetDistanceTo(coordinateB);
(其中坐标 A 和 B 的类型为地理坐标)
See the MDSN 文档 http://msdn.microsoft.com/en-us/library/system.device.location.geocoordinate.getdistanceto.aspx.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)