The haversine formula
For a quick estimate the Earth is treated as a sphere with radius R ≈ 6,371 km. With latitudes φ and longitudes λ in radians:
a = sin²(Δφ/2) + cos φ1 · cos φ2 · sin²(Δλ/2)
d = 2R · atan2(√a, √(1−a))
// JavaScript
function haversineKm(lat1, lon1, lat2, lon2) {
const r = Math.PI / 180, R = 6371.0088;
const a = Math.sin((lat2 - lat1) * r / 2) ** 2 +
Math.cos(lat1 * r) * Math.cos(lat2 * r) * Math.sin((lon2 - lon1) * r / 2) ** 2;
return 2 * R * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
}
The sphere is off by up to about 0.5 %. The main result above is computed on the WGS84 ellipsoid GPS uses, accurate to millimetres; the haversine value and its difference are listed below it. Paris → Berlin, the example, is about 880 km.
Questions
Why does my route planner show a longer distance?
Route planners measure along roads. This is the distance "as the crow flies" – the shortest path over the Earth's surface.
In which format do I enter coordinates?
Latitude first, then longitude, as decimal degrees (48.8566, 2.3522), degrees-minutes-seconds, or simply a Google Maps / OpenStreetMap link.
How do I get miles?
Switch the unit to miles or nautical miles; all values update. One mile is 1.609344 km, one nautical mile 1.852 km.