using System; namespace SimConnect.Lib { /// /// Provides helpers methods for latitude / longitude calculations. /// public static class LatLon { /// /// Defines the approximate radius of the earth in kilometers. /// public const double EarthRadiusKm = 6378.137; // Source: https://stackoverflow.com/questions/639695/how-to-convert-latitude-or-longitude-to-meters /// /// Calculates the distance between two coordinates. /// /// Latitude of point 1 in degrees /// Longitude of point 1 in degrees /// Latitude of point 2 in degrees /// Longitude of point 2 in degrees /// public static double DistanceBetweenInMeters(float lat1, float lon1, float lat2, float lon2) { var distanceLat = lat2 * Math.PI / 180 - lat1 * Math.PI / 180; var distanceLon = lon2 * Math.PI / 180 - lon1 * Math.PI / 180; var a = Math.Sin(distanceLat / 2) * Math.Sin(distanceLat / 2) + Math.Cos(lat1 * Math.PI / 180) * Math.Cos(lat2 * Math.PI / 180) * Math.Sin(distanceLon / 2) * Math.Sin(distanceLon / 2); var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a)); var distanceKm = EarthRadiusKm * c; return distanceKm * 1000; } } }