我正在編寫需要位置感知的Windows Phone 7應用程序。具體而言,我希望某些(c#)代碼在電話進入特定位置(固定)範圍內時運行,例如說0.5英里。我擁有存儲器中所有物理位置的緯度/經度數據。我將使用Geo Coordinate Watcher class來獲取設備的當前座標。現在唯一的技巧是計算用戶是否在任何位置的範圍內。計算一個座標是否在另一個範圍內
謝謝!
更新:作爲承諾這裏的小C#功能,它使用Spherical Law of Cosines計算距離的方法。希望它可以幫助別人。注意:我正在編寫Windows Phone 7應用程序,因此使用了GeoLocation類。如果你使用「regular」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;
}
出於好奇,爲什麼不使用在同一頁上呈現的更簡單(更快)的餘弦定律?作者指出,對於大於1米的距離,建議和準確。 – ctacke 2010-09-20 01:30:20
我剛剛重新閱讀文章,你說得對,Cosines的球形法更簡單。實際上,我實現了這兩個方法來看看它是如何完成的 - 我也會發布該代碼。謝謝... – will 2010-09-20 16:24:44