Haversine Distance Calculator
The great-circle distance between two coordinates, with the initial and final bearings, the midpoint, and the distance on the WGS-84 ellipsoid beside it.
Two points
Type decimal degrees (-74.006), degrees and decimal minutes (40° 42.767′ N), or degrees, minutes and seconds (40° 42′ 46″ N, 40 42 46 N). South and west are negative. A pair such as 40.712776, -74.005974, pasted into either field, is split into both.
Sphere radius
The round figure most haversine code uses.
The route on the globe
Seen from far above the route's midpoint. The dashed line is the equator; the others are every 30° of latitude and longitude.
Great-circle distance
5,570.222 km
- Miles
- 3,461.176 mi
- Nautical miles
- 3,007.679 nmi
- Central angle
- 50.0942°
On the WGS-84 ellipsoid
5,585.234 km
By Vincenty's method. The sphere's figure is 15.011 km (0.269%) shorter.
Bearings and midpoint
- Initial bearing, leaving A
- 51.2° NE
- Final bearing, arriving at B
- 108.3° ESE
52.36844, -41.29031
52° 22′ 6.4″ N, 41° 17′ 25.1″ W
A great circle crosses each meridian at a different angle, so the bearing changes along the way unless the route runs along a meridian or the equator.
Show the working
φ1 = 40.712800°, φ2 = 51.507400°
Δφ = 10.794600°, Δλ = 73.878200°
h = sin²(Δφ/2) + cos φ1 · cos φ2 · sin²(Δλ/2) = 0.1792364335
c = 2 · atan2(√h, √(1 − h)) = 0.8743089279 rad
d = R · c = 6,371 km × 0.8743089279 = 5,570.222 km
How the haversine formula measures a great circle
The haversine of an angle is half its versed sine: hav θ = sin²(θ/2) = (1 − cos θ) / 2. For two points at latitudes φ1 and φ2 and longitudes λ1 and λ2, h = sin²(Δφ/2) + cos φ1 cos φ2 sin²(Δλ/2) is the haversine of the angle between them at the Earth's centre. That angle is c = 2·atan2(√h, √(1 − h)), and the distance is the radius times c. The working panel above shows each step for your two points.
A sphere is a model of the Earth, which bulges at the equator: a degree of latitude spans 110.57 km there and 111.69 km at the poles, and no one radius matches both. Against the WGS-84 ellipsoid, a sphere of 6,371 km makes north–south distances up to 0.56% too long near the equator and up to 0.45% too short near the poles. New York to London comes out 5,570.2 km on the sphere and 5,585.2 km on the ellipsoid, which is why the ellipsoid figure sits beside the result.
Built and maintained by Meet Shah · Last updated
What this tool is used for
- Checking each step of your own haversine code against the working panel, from h to the central angle.
- Finding how far apart two places are as the crow flies, from coordinates copied out of Google Maps or Wikipedia.
- Getting the heading to leave on for a great-circle route, and how far it has turned on arrival.
- Seeing how far a spherical distance is from the WGS-84 figure before choosing one for a dataset.
- Reproducing a figure from MySQL's ST_Distance_Sphere or Google Maps' geometry library by choosing the radius each one uses.
- Measuring a distance on Mars or the Moon from latitude and longitude, with the body's mean radius.
How it works in practice
A worked example
You need the shortest distance and the departure heading from Sydney to Santiago, and on a Mercator wall map the straight line between them runs almost due east.
Point A -33.8688, 151.2093 (Sydney) Point B -33.4489, -70.6693 (Santiago) Radius 6,371 km
Great-circle distance 11,346.715 km Initial bearing 145.3° SE Final bearing 34.5° NE Midpoint -61.77700, -139.36465 On WGS-84 11,368.984 km (the sphere is 0.196% shorter)
The straight line on a Mercator map is a rhumb line, which holds one compass heading, here 89.8°, and runs 12,783.6 km. The great circle is 1,436.9 km shorter. It leaves Sydney heading 145.3°, south-east, reaches its southernmost point, 61.8° S over the Southern Ocean, almost exactly halfway, and arrives in Santiago heading 34.5°, north-east. Mercator stretches the far south so much that the shorter path looks like a long detour on the map. The haversine gives the angle between the cities at the Earth's centre, 102.04° or 1.78099 radians, and 6,371 km times that is the distance.
The edge case that catches people
Points that are nearly opposite each other are where both methods strain. Every great circle from A passes through A's antipode, so near it a small move of B swings the bearing round, and Vincenty's iteration on the ellipsoid can fail to converge when B is within some tens of kilometres of the antipode. On one of GeographicLib's published test lines, an implementation that stops the iteration early returns a length 9 m short, so this page shows no ellipsoid figure there rather than a wrong one. Exactly opposite points have no single bearing or midpoint at all, and the page says so instead of printing one.
When not to use this tool
Not for travel distance or time, which depend on roads, airways and winds; a routing engine answers that. Not where half a per cent matters, such as surveying, land boundaries or a distance quoted to the metre: use the ellipsoid figure beside the result, or GeographicLib, and for local work a projected coordinate system in metres. And not for two points on different datums: a latitude and longitude on an older national datum, such as NAD27 on older US maps, names a slightly different place on WGS-84, so convert both points to one datum first.
Frequently Asked Questions
- What does the haversine formula compute?
- The great-circle distance: the shortest path between two points over a sphere's surface. It computes h = sin²(Δφ/2) + cos φ1 cos φ2 sin²(Δλ/2), the haversine of the angle between the points at the sphere's centre, recovers the angle as c = 2·atan2(√h, √(1 − h)), and multiplies it by the radius.
- Why haversine rather than the spherical law of cosines?
- Rounding near zero. The law of cosines takes the arc-cosine of a number a hair below 1, where floats are coarsest: for points millimetres apart, 64-bit floats can be out by 13 cm and 32-bit ones, as in GPU shaders, by about 3 km. The haversine keeps its precision there; near the antipode it can be out by over 20 cm.
- Which radius should I use?
- 6,371 km unless you are matching another system: Turf.js uses 6,371.0088 km, Google Maps' geometry library 6,378.137 km and MySQL 6,370.986 km. No sphere is the Earth: against WGS-84, 6,371 km runs up to 0.56% long near the equator and 0.45% short near the poles, so the ellipsoid figure is the better one.
- What are the initial and final bearings?
- The heading at each end, in degrees clockwise from true north; a magnetic compass differs by the local declination. A great circle's heading changes as it goes: New York to London leaves at 51.2°, north-east, and arrives at 108.3°, east-south-east. London to Tokyo leaves at 31.7°, passes 70.9° N and arrives at 156.2°.
- Does this account for terrain or roads?
- No. It is the distance over a smooth sphere, so a road or a path is always longer, by however much the network winds. Height changes little: a steady 10% grade adds 0.5% to the length, and a flight cruising at 11 km travels on a sphere 0.17% larger than the ground.
- Which coordinate formats can I type?
- Decimal degrees, with a minus for south and west; degrees and decimal minutes; or degrees, minutes and seconds with ° ′ ″ or ' ", colons or spaces. The hemisphere letter can go before or after. A pair pasted from Google Maps or Wikipedia splits into both fields, and a W in the latitude field is reported, not guessed.
Common errors and gotchas
- Reading the result as a travel distance. Roads, paths and airways are longer than the great circle by however much they wind.
- Swapping latitude and longitude. GeoJSON and KML write longitude first, while Google Maps and Leaflet write latitude first, so a pair copied from one to the other lands somewhere else.
- Passing degrees to sin and cos, which take radians in nearly every language. Multiply by π/180 first, or the answer is a wrong number that still looks plausible.
- Treating the spherical figure as exact. Against the WGS-84 ellipsoid it runs from 0.45% short to 0.56% long with the direction and the latitude, which is 15 km on New York to London.
- Dropping the minus of a southern latitude or a western longitude. −33.8688, 151.2093 is Sydney; without the minus it is a point in the Pacific 7,532 km due north.
- Reading decimal degrees as minutes. 40.30° is 40° 18′, not 40° 30′, and the 12 minutes between them are 22 km of latitude.