Haversine Calculator - Great Circle Distance

Compute great-circle distance between two latitude/longitude points using the haversine formula. Explains spherical vs flat-Earth distance, accuracy, bearing, and code.

°°
°°
km

What is the Haversine Formula?

The haversine formula computes the shortest distance between two points on the surface of a sphere — for our purposes, Earth — given their latitude and longitude. It returns the great-circle distance: the length of the shortest curve that lies entirely on the sphere's surface between the two points, also called an orthodrome.

It is the formula that GPS navigation, flight-route planning, ride-sharing apps, delivery optimization, weather radar overlays, and almost every map API rely on whenever they need a quick 'how far apart are these two points?' answer. It accounts for Earth's curvature, so it stays accurate even for intercontinental distances where treating the planet as flat would be off by hundreds of kilometres.

The formula is derived from the law of haversines in spherical trigonometry. The haversine function itself is defined as:

hav(θ) = sin²(θ/2) = (1 − cos(θ))/2

Key properties and applications of the haversine formula include:

  • Spherical geometry: works on Earth's curved surface, not a flat map projection that distorts distance
  • Navigation: essential for flight paths, shipping routes, drone planning, GPS guidance
  • Accuracy: gives results within 0.5% of the true distance for the spherical model — good enough for almost every consumer application
  • Stable arithmetic: the squared-sine form avoids the precision loss that plagues the equivalent 'spherical law of cosines' formula at small distances
  • Universal range: works for any two points on Earth, including antipodal pairs (opposite sides of the planet)

Modern applications include ride-sharing matching, delivery time estimation, hyperlocal advertising, weather forecasting, and any system that needs to determine proximity or compute travel distances between geographical locations.

How does the Haversine Formula work?

The haversine formula computes the great-circle distance between two points on a sphere from their latitude and longitude. It uses sine, cosine, and arcsine, and assumes the sphere has a known radius — typically Earth's mean radius of 6,371 km.

The full haversine formula:

d = 2r · arcsin(√(sin²(Δφ/2) + cos(φ₁) · cos(φ₂) · sin²(Δλ/2)))

Where:

  • d = great-circle distance between the two points
  • r = radius of the Earth (approximately 6,371 km or 3,959 miles)
  • φ₁, φ₂ = latitude of point 1 and point 2 (in radians)
  • Δφ = φ₂ − φ₁ (difference in latitude)
  • Δλ = λ₂ − λ₁ (difference in longitude)
  • λ₁, λ₂ = longitude of point 1 and point 2 (in radians)

The formula works by:

  • Converting latitude and longitude from degrees to radians (multiply by π/180)
  • Computing the half-angle haversines of the latitude and longitude differences
  • Combining them with cos(φ₁)·cos(φ₂) to project onto the sphere
  • Taking the inverse haversine (arcsine of the square root) to recover the central angle in radians
  • Multiplying the central angle by the radius to get the actual distance along the surface

Because the calculation works with half-angle sines instead of cosines of the full angle, it remains numerically stable even when the two points are very close together — a property the older spherical-law-of-cosines formula lacks.

Common Haversine Distance Examples

Some typical great-circle distances computed with the haversine formula:

  • New York to Los Angeles: ~3,944 km (2,451 miles)
  • London to Tokyo: ~9,560 km (5,940 miles) — and a real flight follows a polar route close to this geodesic
  • Sydney to Melbourne: ~713 km (443 miles)
  • Paris to Rome: ~1,103 km (685 miles)
  • Hanoi to Ho Chi Minh City: ~1,137 km (706 miles)

Frequently Asked Questions

Because Earth is round, not flat. The Pythagorean distance √((Δx)² + (Δy)²) works on a plane, where coordinates are independent of position. Latitude and longitude are angles on a sphere, so a 1° change in longitude at the equator covers ~111 km of actual ground but only ~78 km at 45° latitude and ~0 km at the poles. If you treat lat/lon as plain (x, y) coordinates and apply Pythagoras, you'll be wildly wrong away from the equator. For tiny distances (a few kilometres in one country) the equirectangular approximation d ≈ r·√(Δφ² + (cos(φ_mean)·Δλ)²) is close enough; for anything continental, you need either haversine or a full ellipsoidal formula like Vincenty's. Haversine accounts for the curvature properly: it computes the arc length between the two points along the surface of a sphere, which is the actual path you'd walk (or fly) on a perfectly round Earth. Without it, your map app would tell you that London is closer to Sydney via Singapore than via Hong Kong, when in fact it's the other way around.

Haversine assumes Earth is a perfect sphere of radius 6,371 km. The real Earth is an oblate spheroid — wider around the equator (6,378 km) than between the poles (6,357 km) — so the formula is off by up to ~0.5% in the worst case. For a 10,000 km flight that's about 50 km of error, more than enough to matter to a pilot navigating with a paper chart but invisible to a Uber driver routing across town. For tasks that need higher accuracy — surveying, geodesy, missile guidance, plate-tectonic tracking — engineers use Vincenty's formulae, which treat Earth as an ellipsoid and converge to centimetre precision. For everyday applications (delivery distance, ride matching, social-media 'nearby people') haversine is plenty. The other source of error is the choice of radius: 6,371 km is the mean radius (volume-equivalent sphere); if you happen to be navigating near the equator, using 6,378 km gives slightly better results, and near the poles 6,357 km does. Most code just uses 6,371 km because the difference is well within haversine's intrinsic error.

A great circle is any circle on a sphere whose centre coincides with the sphere's centre — the equator is one, every meridian (pair of opposite longitude lines) is one, and the unique geodesic between any two non-antipodal points lies on exactly one great circle. The great-circle distance, which haversine computes, is the shortest path between two points on the sphere. A rhumb line (or loxodrome) is a path that crosses every meridian at the same constant angle — a 'fixed compass bearing'. Rhumb lines look like straight lines on a Mercator-projection map, which is why old sailors loved them: 'steer due north-east' is easy to follow without constant heading corrections. But a rhumb line is longer than the great-circle route, sometimes dramatically so. New York to Tokyo by rhumb line is about 11,500 km; by great circle it's 10,800 km. Modern airliners fly the great circle even though it requires continuously adjusting heading. Haversine returns great-circle distance; for rhumb-line distance you'd use a different formula based on the Mercator-projected difference.

Because that IS the shortest path on a sphere. New York is at about 40° N, Tokyo at 35° N — both well in the northern hemisphere. If you draw a straight line between them on a globe, it sweeps north over Alaska, sometimes over Russia, never over the central Pacific. The Mercator projection — the one almost every wall map uses — radically distorts polar regions: Greenland looks bigger than Africa even though it's 14 times smaller. So a 'straight' line on a Mercator map (the rhumb line) curves down and east, hugging the Pacific. The true geodesic, which the haversine formula represents, curves over the pole. This is also why intercontinental airline routes look weirdly curved on flat maps but appear straight on a globe. The same logic applies to Sydney–Santiago (over Antarctica), Johannesburg–Sydney (over the Antarctic Ocean), and London–Auckland (over the Arctic). The haversine formula doesn't tell you the path, only the distance, but the distance only makes sense in the context of that great-circle path.

The standard convention is 6,371,000 m (6,371 km, or 3,959 miles, or 3,440 nautical miles). This is the mean volumetric radius — the radius of a sphere with the same volume as Earth. Three more precise values exist: the equatorial radius (6,378.137 km, used in the WGS84 datum), the polar radius (6,356.752 km), and the mean radius (6,371.009 km, the geographic average). The differences are small (~0.3% peak-to-peak) but they matter when you compare with reference data. Pilots and ship navigators often use 6,371 km exactly and accept the small error; geodetic surveys use the WGS84 ellipsoid. For miles, the haversine formula stays the same — just plug in r = 3,959 (statute miles) or r = 3,440 (nautical miles) and the answer comes out in that unit. The simplest, most popular choice is r = 6,371,008.8 m (the IUGG mean radius), and almost every haversine implementation you'll find online hardcodes this or a rounded 6,371 km.

Haversine gives distance; bearing is a separate calculation. The initial bearing from point 1 (φ₁, λ₁) to point 2 (φ₂, λ₂) is θ = atan2(sin(Δλ)·cos(φ₂), cos(φ₁)·sin(φ₂) − sin(φ₁)·cos(φ₂)·cos(Δλ)), in radians. Convert to degrees and normalise to [0, 360) by adding 360 and taking modulo. The result is the compass direction you'd start walking — 0° = north, 90° = east, 180° = south, 270° = west. On a great-circle route, the bearing actually changes continuously along the path (this is why your in-flight map shows your heading slowly drifting), so the formula gives the initial bearing at point 1. For the final bearing arriving at point 2, swap the two points, compute the initial bearing, and add 180° modulo 360°. Use atan2, not atan(y/x) — the four-quadrant version handles every combination of signs correctly. This formula plus haversine are the workhorses of nearly every geographic library: PostGIS, Turf.js, geopy, GeoTools.

JavaScript: function haversine(lat1, lon1, lat2, lon2) { const R = 6371; const toRad = d => d * 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.asin(Math.sqrt(a)); }. Python is identical, swap Math.sin for math.sin. Common pitfalls: forgetting to convert degrees to radians (sin and cos take radians, but coordinates are reported in degrees); mixing up latitude and longitude order (some libraries use (lon, lat), most APIs use (lat, lon)); using Math.asin instead of the more numerically stable atan2(sqrt(a), sqrt(1-a)) for distances near antipodes; choosing R = 6378 (equatorial) when 6371 (mean) is the convention. For production use, prefer a battle-tested library: in Python, scipy.spatial.distance, geopy.distance, or the haversine package; in JavaScript, Turf.js or @turf/distance. Roll-your-own works fine but has these landmines.

Three scenarios. First, when you need high precision over short distances — for sub-millimetre survey work or land-titling, haversine's spherical assumption introduces too much error and you should use Vincenty's inverse formula on a WGS84 ellipsoid, or a local projected coordinate system (UTM, state plane). Second, when you only need an approximate distance over a small area — like 'are these two points within 1 km of each other inside one city?'. Then the equirectangular approximation d ≈ R·√(Δφ² + (cos(φ_mean)·Δλ)²) is two trig calls instead of haversine's six, runs in 1/3 the time, and is accurate to better than 0.1% within a few hundred km. For massive batch processing (think: matching every Uber driver to every passenger), the speedup adds up. Third, when the points are very close to being antipodal (opposite sides of the planet, separation > 179.5°), naive haversine can lose precision — atan2-based formulations are more robust there. Most apps never hit any of these edge cases, so haversine remains the right default.
Haversine Calculator - Great Circle Distance — Compute great-circle distance between two latitude/longitude points using the haversine formula. Explains spherical vs f
Haversine Calculator - Great Circle Distance