// calculate distance in km using Haversine formula // see http://www.movable-type.co.uk/scripts/latlong.html public static double GetDistanceKm(Models.Location from, Models.Location to) { double delrad = Math.Acos(Math.Sin(ToRadians(from.Latitude)) * Math.Sin(ToRadians(to.Latitude)) + Math.Cos(ToRadians(from.Latitude)) * Math.Cos(ToRadians(to.Latitude)) * Math.Cos(ToRadians(to.Longitude - from.Longitude))); return(EarthRadiusKm * delrad); }
// calculate bearing in degrees // see http://www.movable-type.co.uk/scripts/latlong.html public static double GetBearing(Models.Location from, Models.Location to) { var lat1 = ToRadians(from.Latitude); var lat2 = ToRadians(to.Latitude); var dLon = ToRadians(to.Longitude - from.Longitude); var y = Math.Sin(dLon) * Math.Cos(lat2); var x = Math.Cos(lat1) * Math.Sin(lat2) - Math.Sin(lat1) * Math.Cos(lat2) * Math.Cos(dLon); var brng = Math.Atan2(y, x); return(ToDegrees(brng)); }
public const double KmToMiles = 0.621371; // to multiply instead of divide // calculate distance in miles using Haversine formula // see http://www.movable-type.co.uk/scripts/latlong.html public static double GetDistanceMiles(Models.Location from, Models.Location to) { return(GetDistanceKm(from, to) * KmToMiles); }