【发布时间】:2011-04-10 12:31:51
【问题描述】:
我正在编写一个需要感知位置的 Windows Phone 7 应用。具体来说,我希望在手机进入特定位置的(固定)范围内(例如 0.5 英里)时运行一些(c#)代码。我拥有内存中物理位置的所有纬度/经度数据。我将使用Geo Coordinate Watcher class 来获取设备的当前坐标。现在唯一的技巧是计算用户是否在任何位置的范围内。
谢谢!
更新:正如所承诺的,这里是使用Spherical Law of Cosines 计算距离方法的小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;
}
【问题讨论】:
-
出于好奇,您为什么不使用更简单(更快)的余弦空间定律,在您访问haversine 的Java 源代码的同一页面上提供?作者指出,对于> 1m的距离,推荐且准确。
-
我刚刚重读了这篇文章,你是对的,余弦球定律更简单。事实上,我实现了两者以查看它是如何完成的 - 我也会发布该代码。谢谢...
标签: c# windows-phone-7 location