Calculating Distance Discrepancy between GeoCoordinates
While attempting to calculate the distance between GeoCoordinates, it has been observed that the results obtained differ from those generated by other applications. The calculation method employed in C#:
public static double Calculate(double sLatitude,double sLongitude, double eLatitude, double eLongitude) { ... }
is based on the Haversine formula. However, the results yield an average of 3.3 miles against the 3.5 miles obtained by other applications.
Discrepancy Root Cause
It is postulated that the discrepancy may not be attributed to the calculation method itself but rather to the implementation details.
Potential Solution
For more accurate distance calculation, consider utilizing the GeoCoordinate class introduced in .NET Framework 4. This class provides the GetDistanceTo method, which directly calculates the distance between two GeoCoordinate objects:
// Create GeoCoordinate objects var sCoord = new GeoCoordinate(sLatitude, sLongitude); var eCoord = new GeoCoordinate(eLatitude, eLongitude); // Calculate distance in meters using GetDistanceTo method var distanceMeters = sCoord.GetDistanceTo(eCoord); // Convert distance from meters to miles (if desired) var distanceMiles = distanceMeters / 1609.34;
Note: Don't forget to add a reference to the System.Device namespace.
The above is the detailed content of Why Do My GeoCoordinate Distance Calculations Differ from Other Applications?. For more information, please follow other related articles on the PHP Chinese website!