To find the straight-line distance between two places from their coordinates, you need a formula that follows the curve of the Earth. The most common one is the haversine formula. From New York City Hall to central London it gives 5,570.23 km. The more precise Vincenty method gives 5,585.24 km. This guide explains both, with a worked example and code you can copy.
If you just need the answer, use the distance calculator: enter two points or click them on the map.
Why you cannot use Pythagoras
Latitude and longitude are angles, not distances on a flat grid. A degree of latitude is roughly 111 km everywhere, but a degree of longitude shrinks from about 111 km at the equator to zero at the poles. Treating degrees as x and y on a flat map gives wrong results, and the error grows with distance and latitude. See how many miles are in a degree for the numbers.
The shortest path between two points on a sphere is part of a great circle, a circle whose centre is the centre of the Earth. The haversine formula measures the angle along that circle and multiplies it by the Earth's radius.
The haversine formula
With latitudes φ1, φ2 and longitudes λ1, λ2, all in radians:
a = sin²(Δφ / 2) + cos φ1 × cos φ2 × sin²(Δλ / 2)
c = 2 × atan2(√a, √(1 − a))
d = R × c
- Δφ is the difference in latitude and Δλ the difference in longitude.
- a is the square of half the straight-line (chord) distance between the points, on a sphere of radius 1.
- c is the angle between the two points as seen from the Earth's centre, in radians.
- R is the Earth's radius. We use the mean radius, 6,371.0088 km.
The name comes from the "haversine" function, hav(θ) = sin²(θ/2). Writing the formula with it keeps the calculation accurate for very short distances, where other spherical formulas can lose precision to rounding.
Worked example: New York City Hall to London
Point 1, New York City Hall: 40.712776, -74.005974. Point 2, London: 51.507351, -0.127758.
- Differences in degrees. Δφ = 51.507351 − 40.712776 = 10.794575°. Δλ = -0.127758 − (-74.005974) = 73.878216°.
- Convert to radians (multiply by π / 180). φ1 = 0.710572, φ2 = 0.898973, Δφ = 0.188401, Δλ = 1.289418.
- First term. sin²(Δφ / 2) = 0.008848.
- Second term. cos φ1 = 0.757989, cos φ2 = 0.622414 and sin²(Δλ / 2) = 0.361160. Their product is 0.170389.
- Add them. a = 0.008848 + 0.170389 = 0.179237.
- Central angle. c = 2 × atan2(√0.179237, √0.820763) = 0.874310 radians, which is 50.094°.
- Distance. d = 6,371.0088 × 0.874310 = 5,570.23 km.
That is about 3,461 miles. Using R = 6,371 km instead gives 5,570.227 km, only about 8 m less, so the choice of rounded radius hardly matters.
Haversine in JavaScript
This short function returns the distance in kilometres:
// Great-circle distance between two points, in kilometres
function haversineKm(lat1, lon1, lat2, lon2) {
const R = 6371.0088; // mean Earth radius in km
const toRad = (deg) => deg * Math.PI / 180;
const dLat = toRad(lat2 - lat1);
const dLon = toRad(lon2 - lon1);
const a = Math.sin(dLat / 2) ** 2 +
Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) * Math.sin(dLon / 2) ** 2;
return 2 * R * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
}
haversineKm(40.712776, -74.005974, 51.507351, -0.127758); // 5570.23...
For miles, multiply the result by 0.621371 or use R = 3,958.8 miles. Python, SQL and spreadsheet versions follow the same steps: convert to radians, compute a, then c, then multiply by the radius.
Vincenty: the ellipsoid method
The Earth is not a perfect sphere. It is slightly flattened: on the WGS84 model the polar radius (6,356.752 km) is about 21.4 km shorter than the equatorial radius (6,378.137 km), and GPS coordinates are defined on the WGS84 ellipsoid (see what WGS84 is). Haversine ignores this, so its results are slightly off.
The Vincenty inverse formula works on the ellipsoid itself. It is iterative: it repeats a calculation until the answer stops changing. It is much longer than haversine, but it takes the flattening into account, so it gives the distance on the same model of the Earth that GPS uses.
For our example, Vincenty gives 5,585.24 km (3,470.51 miles or 3,015.79 nautical miles). Haversine is 15.00 km shorter, a difference of about 0.27%.
How big is the difference in practice?
| Route | Haversine | Vincenty (WGS84) | Difference |
|---|---|---|---|
| NYC City Hall to Statue of Liberty | 4,170.60 m | 4,174.88 m | 4.28 m (0.10%) |
| London to Eiffel Tower, Paris | 341.29 km | 341.64 km | 0.36 km (0.10%) |
| NYC City Hall to London | 5,570.23 km | 5,585.24 km | 15.00 km (0.27%) |
| London to Tokyo | 9,558.73 km | 9,582.31 km | 23.58 km (0.25%) |
The difference depends on the direction of the route and the latitudes involved, but in these examples it stays below 0.3%.
Which one should I use?
- Haversine is enough for most apps: nearest-store searches, fitness tracks, travel estimates and sorting by distance. It is fast and has no special cases.
- Vincenty is better when accuracy matters: surveying, aviation and marine planning, or comparing with official distances. Note that it can fail to converge for points that are almost exactly on opposite sides of the Earth; good software falls back to another method in that case.
Our distance calculator gives the Vincenty distance in kilometres, miles and nautical miles. For nearly opposite points it falls back to haversine and tells you so.
Common mistakes
- Forgetting to convert to radians. JavaScript, Python and spreadsheets expect radians in
sinandcos. Passing degrees gives nonsense. - Swapping latitude and longitude. Keep the order consistent for both points. See latitude vs longitude.
- Using DMS values directly. Convert 40° 42' 46" to 40.7128 first, for example with DMS to DD.
- Mixing units. The result is in the same unit as R. Use kilometres for both, or miles for both.
- Expecting road distance. These formulas give the distance as the crow flies, not along roads.
Related calculations
- Bearing calculator: the compass direction from one point to another. From New York City Hall, London lies at an initial bearing of about 51°, roughly northeast. Read how to calculate bearing.
- Midpoint calculator: the point halfway along the great circle.
- Area calculator: the area and perimeter of a shape drawn on the map.