Distance Between Two Coordinates: Haversine Explained

Published

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

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.

  1. Differences in degrees. Δφ = 51.507351 − 40.712776 = 10.794575°. Δλ = -0.127758 − (-74.005974) = 73.878216°.
  2. Convert to radians (multiply by π / 180). φ1 = 0.710572, φ2 = 0.898973, Δφ = 0.188401, Δλ = 1.289418.
  3. First term. sin²(Δφ / 2) = 0.008848.
  4. Second term. cos φ1 = 0.757989, cos φ2 = 0.622414 and sin²(Δλ / 2) = 0.361160. Their product is 0.170389.
  5. Add them. a = 0.008848 + 0.170389 = 0.179237.
  6. Central angle. c = 2 × atan2(√0.179237, √0.820763) = 0.874310 radians, which is 50.094°.
  7. 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?

RouteHaversineVincenty (WGS84)Difference
NYC City Hall to Statue of Liberty4,170.60 m4,174.88 m4.28 m (0.10%)
London to Eiffel Tower, Paris341.29 km341.64 km0.36 km (0.10%)
NYC City Hall to London5,570.23 km5,585.24 km15.00 km (0.27%)
London to Tokyo9,558.73 km9,582.31 km23.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?

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

Related calculations

Frequently asked questions

What is the haversine formula?

It is a formula for the great-circle distance between two points on a sphere, given their latitudes and longitudes. It is short, stable for small distances and accurate to a fraction of a percent on the real Earth.

What Earth radius should I use?

A common choice is the mean Earth radius of 6,371.0088 km (often rounded to 6,371 km). For New York to London, the two values give results that differ by only about 8 m.

How accurate is haversine compared with Vincenty?

Haversine treats the Earth as a sphere, so it is slightly off. From New York City Hall to London it gives 5,570.23 km, while the Vincenty formula on the WGS84 ellipsoid gives 5,585.24 km, a difference of about 0.27%.

Is this the driving distance?

No. Both formulas give the shortest distance over the Earth's surface, as the crow flies. Road, rail and flight-path distances are longer.