Skip to content

Instantly share code, notes, and snippets.

@tanaka-takayoshi
Created September 10, 2012 09:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanaka-takayoshi/3689904 to your computer and use it in GitHub Desktop.
Save tanaka-takayoshi/3689904 to your computer and use it in GitHub Desktop.
GetDistance between two points in Windows Phone SDK
public static class DistanceCalc
{
/// <summary>
/// 長半径(赤道半径) at WGS84 (GPS)
/// </summary>
private const double equationalRaius = 6378137.000;
/// <summary>
/// 短半径(極半径) at WGS84 (GPS)
/// </summary>
private const double polarRadius = 6356752.314245;
/// <summary>
/// 第一離心率の二乗
/// </summary>
private const double eSquared = (equationalRaius * equationalRaius - polarRadius * polarRadius)
/ (equationalRaius * equationalRaius);
/// <summary>
/// 子午線曲率半径の分子
/// </summary>
private const double numerator = equationalRaius * (1 - eSquared);
public static double Calc(double lon1, double lat1, double lon2, double lat2)
{
//度からラジアンに変換
var lonRad1 = lon1 * Math.PI / 180;
var latRad1 = lat1 * Math.PI / 180;
var lonRad2 = lon2 * Math.PI / 180;
var latRad2 = lat2 * Math.PI / 180;
var deltaLat = latRad1 - latRad2;
var deltaLon = lonRad1 - lonRad2;
var avgLat = (latRad1 + latRad2) / 2;
var sin = Math.Sin(avgLat);
var doubleu = Math.Sqrt(1 - eSquared * sin * sin);
//子午線曲率半径
var meridian = numerator / (doubleu * doubleu * doubleu);
//卯酉線曲率半径
var primeVertical = equationalRaius / doubleu;
var deltaY = deltaLat * meridian;
var deltaX = deltaLon * primeVertical * Math.Cos(avgLat);
return Math.Sqrt(deltaX * deltaX + deltaY * deltaY);
}
}
@tanaka-takayoshi
Copy link
Author

上のサンプルはHubeny の公式を使った計算処理。

GeoCoordinate.GetDistanceTo メソッドもあるけど、これは地球を真球と仮定するHaversine の式で出している。
http://msdn.microsoft.com/ja-jp/library/system.device.location.geocoordinate.getdistanceto%28v=vs.92%29.aspx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment