public static double DistanceInKm(Position pos1, Position pos2) { return Distance(pos1, pos2, DistanceType.Kilometers); }
public static double DistanceInMeters(Position pos1, Position pos2) { return DistanceInKm(pos1, pos2) * 1000; }
/// <summary> /// Returns the distance in miles or kilometers of any two /// latitude / longitude points. /// </summary> /// <param name=”pos1″></param> /// <param name=”pos2″></param> /// <param name=”type”></param> /// <returns></returns> public static double Distance(Position pos1, Position pos2, DistanceType type) { double R = (type == DistanceType.Miles) ? 3960 : 6371; double dLat = GeoMath.ToRadian(pos2.Latitude - pos1.Latitude); double dLon = GeoMath.ToRadian(pos2.Longitude - pos1.Longitude); double a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Cos(GeoMath.ToRadian(pos1.Latitude)) * Math.Cos(GeoMath.ToRadian(pos2.Latitude)) * Math.Sin(dLon / 2) * Math.Sin(dLon / 2); double c = 2 * Math.Asin(Math.Min(1, Math.Sqrt(a))); double d = R * c; return d; }