To calculate the distance between two coordinates (points) in Delphi, you can use the Haversine formula, which is a method used to calculate the great-circle distance between two points on a sphere based on their longitudes and latitudes.
Here is a function that you can use to calculate the distance between two coordinates in Delphi:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
function CalculateDistance(lat1, lon1, lat2, lon2: Double): Double; const R = 6371; // Earth’s radius in kilometers var dLat, dLon: Double; a, c: Double; begin dLat := (lat2 – lat1) * (Pi / 180); dLon := (lon2 – lon1) * (Pi / 180); lat1 := lat1 * (Pi / 180); lat2 := lat2 * (Pi / 180); a := Sin(dLat/2) * Sin(dLat/2) + Sin(dLon/2) * Sin(dLon/2) * Cos(lat1) * Cos(lat2); c := 2 * ArcTan2(Sqrt(a), Sqrt(1–a)); Result := R * c; end; |
This function takes in four parameters:
lat1
: The latitude of the first coordinatelon1
: The longitude of the first coordinatelat2
: The latitude of the second coordinatelon2
: The longitude of the second coordinate
It returns the distance between the two coordinates, in kilometers.
To use this function, you would call it like this:
1 |
distance := CalculateDistance(40.689247, –74.044502, 51.508530, –0.076132); |
This would calculate the distance between the coordinates for the Statue of Liberty in New York (40.689247, -74.044502) and the London Eye in London (51.508530, -0.076132). The function would return the distance between the two points in kilometers.
Leave a Reply