Trail Notes

How to Calculate the Distance and Bearing Between Two Coordinates

How far, and which way: by calculator, by a quick estimate, by the haversine formula, and in a spreadsheet.

Coords Reading time: 9 minutes Updated By the GPX Analyzer project

How Do You Find the Distance and Bearing Between Two Coordinates?

The quick way is to enter the two positions into the Distance & Bearing Calculator, which gives the distance, the bearing to set off on, the bearing you arrive on, and the bearing back. By hand, the distance comes from the haversine formula, which treats the Earth as a sphere and is right to within about half a percent, and the bearing from a companion formula using the same inputs. For a rough figure with no trigonometry beyond one cosine, a degree of latitude is about 111 km and a degree of longitude is 111 km times the cosine of the latitude. All three methods need the positions in decimal degrees, with south and west as negative numbers, and all of them give a true bearing, not a magnetic one.

Three Examples, Three Scales

A long straight desert road running across a flat plain toward a single distant peak on the horizon in low golden light

How much the method matters depends on how far apart the points are, so this guide carries three pairs of points through every method. The figures in this table are the calculator's, measured over the WGS 84 ellipsoid, the model of the Earth that GPS positions refer to.

From and toDistanceBearing leavingBearing arriving
A walk: 34.0500, −116.1500 to 34.0750, −116.13003.33 km034°034°
A drive: Los Angeles (34.0522, −118.2437) to Las Vegas (36.1699, −115.1398)367.7 km049°051°
An ocean: New York (40.7128, −74.0060) to London (51.5074, −0.1278)5,585 km051°108°

These are straight-line distances over the surface, not distances by road or trail.

The Fastest Way: Use the Calculator

  1. Open the Distance & Bearing Calculator and leave it on Between A and B.
  2. Set Point A. Type the latitude and longitude, paste a whole position into either box, or click the map. Decimal degrees, degrees minutes seconds, and degrees decimal minutes are all read.
  3. Set Point B the same way. To use points you already have, choose Waypoints from a GPX File and press A on one waypoint and B on another.
  4. Read the result. The headline gives the distance with the initial and final bearings. The table under it adds the bearing back, the midpoint, the rhumb line, and the distance on a sphere.

If your positions are in UTM or MGRS, turn them into latitude and longitude first with the Coordinate Converter. The rest of this guide is what the calculator is doing, and how to do it without one.

A Quick Estimate

Latitude lines are evenly spaced, so a degree of latitude is always close to 111 km. Longitude lines close up toward the poles, so a degree of longitude is 111 km at the equator and shrinks with the cosine of the latitude.

At latitude1° of latitude1° of longitude
0°110.6 km111.3 km
30°110.9 km96.5 km
45°111.1 km78.8 km
60°111.4 km55.8 km
75°111.6 km28.9 km

That gives a method for short distances:

  1. North–south distance = difference in latitude × 111.2 km.
  2. East–west distance = difference in longitude × 111.2 km × cos(the latitude half way between).
  3. Distance = the square root of the sum of their squares.
  4. Bearing = the angle whose tangent is east divided by north, measured clockwise from north.

For the walk: 0.025° of latitude is 2.78 km north, and 0.020° of longitude at 34° is 1.84 km east. That makes 3.33 km on a bearing of 033.5°, the same as the calculator to the nearest ten meters. For the drive it gives 235.5 km north and 282.3 km east: 367.6 km on 050°, within about 100 m of the true distance.

It stops working when the two points are far apart. For New York to London it gives 5,820 km on 078°, which is 235 km more than the shortest path. What it has found is the rhumb line, the path of constant bearing, and over an ocean that is a different line.

The Haversine Formula, Step by Step

The haversine formula gives the shortest distance between two points on a sphere. Write φ for latitude and λ for longitude, both in radians (degrees × π ÷ 180), and R for the radius of the Earth, 6,371 km.

a = sin²(Δφ / 2) + cos φ1 × cos φ2 × sin²(Δλ / 2)
c = 2 × atan2(√a, √(1 − a))
d = R × c

Here it is for Los Angeles to Las Vegas:

  1. Differences. Δφ = 36.1699 − 34.0522 = 2.1177° = 0.036961 radians. Δλ = −115.1398 − (−118.2437) = 3.1039° = 0.054173 radians.
  2. a = sin²(0.018480) + cos(34.0522°) × cos(36.1699°) × sin²(0.027087) = 0.00083209.
  3. c = 2 × atan2(√a, √(1 − a)) = 0.057700 radians. This is the angle between the two points as seen from the center of the Earth.
  4. d = 6,371 × 0.057700 = 367.606 km.

The calculator's figure, on the ellipsoid, is 367.749 km. The sphere is 142 m short over 368 km, or 0.04%. That is the size of error haversine makes, and for most purposes it is nothing.

The Bearing Formula

The bearing on which the shortest path leaves the first point uses the same latitudes and the same difference in longitude:

y = sin Δλ × cos φ2
x = cos φ1 × sin φ2 − sin φ1 × cos φ2 × cos Δλ
bearing = atan2(y, x), in degrees, plus 360 if it is negative

For Los Angeles to Las Vegas, y = 0.043711 and x = 0.037616, and atan2 gives 49.29°. The calculator, on the ellipsoid, gives 49.41°. A hand compass cannot be read that finely.

Two things about the result. atan2 returns an angle between −180° and 180°, so a bearing west of north comes out negative and needs 360 added: −45° is 315°. And this is the bearing at the start of the line only, for a reason the next section but one explains.

In a Spreadsheet

Put the first point's latitude in A1 and its longitude in B1, and the second point's in A2 and B2, all in decimal degrees. These work in Excel and in Google Sheets.

Distance in kilometers:

=6371*2*ASIN(SQRT(SIN(RADIANS(A2-A1)/2)^2+COS(RADIANS(A1))*COS(RADIANS(A2))*SIN(RADIANS(B2-B1)/2)^2))

Initial bearing in degrees:

=MOD(DEGREES(ATAN2(COS(RADIANS(A1))*SIN(RADIANS(A2))-SIN(RADIANS(A1))*COS(RADIANS(A2))*COS(RADIANS(B2-B1)),SIN(RADIANS(B2-B1))*COS(RADIANS(A2)))),360)

With the Los Angeles and Las Vegas coordinates these give 367.606 and 49.29, the same as the working above. For miles, replace 6371 with 3958.8; for nautical miles, 3440.1.

The trap is ATAN2. In both Excel and Google Sheets it takes x first and y second, the reverse of the order used in the formula above and in most programming languages. The bearing formula here already has them the spreadsheet way round. Swap them and the answer is a mirror image: 40.71° in place of 49.29°.

Why the Bearing Changes Along the Way

The shortest path between two points on a globe is part of a great circle, and a great circle crosses each meridian at a different angle. To stay on it you have to keep changing course.

New York to London on a Mercator grid: the shortest path curves north and is 5,585 km; the rhumb line is straight, holds 078 degrees, and is 5,810 km 80°W 70°W 60°W 50°W 40°W 30°W 20°W 10°W 0° 40°N 50°N 60°N New York London Shortest path: 5,585 km, leaves on 051°, arrives on 108° Rhumb line: 5,810 km, 078° all the way

New York to London on a Mercator grid, where a line of constant bearing is straight. The shortest path, drawn from points the calculator worked out, bows toward the pole.

  • Initial and final bearing. From New York the shortest path leaves on 051° and arrives in London on 108°. On the drive to Las Vegas it leaves on 049° and arrives on 051°. On the walk the two differ by a hundredth of a degree.
  • The bearing back is the final bearing turned round, not the initial bearing plus 180°. From London back to New York it is 288°, where adding 180° to 051° would give 231°.
  • The rhumb line is the path that holds one bearing all the way. New York to London on a rhumb line is 078° throughout and 5,810 km, about 225 km longer. It is what you get by steering a single compass course.

Over the distances people walk, ride, and drive in a day, the two paths are the same line and one bearing describes it. The drive's rhumb line is 15 meters longer than its shortest path.

A Point from a Bearing and a Distance

The reverse question is where you end up after travelling a given distance on a given bearing. In the calculator, choose B from a bearing & distance, set Point A, and type the two values. From 34.0500, −116.1500 on a bearing of 045° for 10 km, Point B is 34.113723, −116.073358.

On a sphere, with θ for the bearing and δ for the distance divided by R, all in radians:

φ2 = asin(sin φ1 × cos δ + cos φ1 × sin δ × cos θ)
λ2 = λ1 + atan2(sin θ × sin δ × cos φ1, cos δ − sin φ1 × sin φ2)

For the same example the sphere formula puts the point 23 meters from the calculator's answer. This is the sum behind projecting a waypoint and behind dead reckoning.

Why Two Calculators Give Different Answers

  • Sphere or ellipsoid. The Earth is flattened at the poles. A sphere of radius 6,371 km reads up to 0.56% long on a north–south line at the equator and up to 0.45% short near the poles. An east–west line at the equator comes out 0.11% short. Most apps and websites use the sphere; survey-grade tools, and the calculator here, use the ellipsoid.
  • Which radius. Some sphere calculators use the equatorial radius, 6,378.137 km, in place of the mean radius of 6,371. Every distance is then 0.11% longer.
  • Rounded coordinates. Four decimal places of a degree is about 11 meters. Two calculators fed positions rounded differently will differ by that much before any formula is applied.
  • Path or straight line. A distance from a map app is usually along roads. The distance of a recorded track is the sum of the gaps between its points. Neither is the straight line between the ends.
  • True or magnetic. A bearing from a formula is true. A bearing read from a compass is magnetic and differs by the local declination; Magnetic Declination Explained covers the correction.

The calculator shows the sphere figure beside the ellipsoid one, so you can tell which a given app is using.

Common Mistakes

  • Degrees fed to a function that wants radians. SIN and COS in a spreadsheet or a programming language take radians. The result is a wildly wrong distance, not an error message.
  • A missing minus sign. West longitudes and south latitudes are negative. Los Angeles at +118 is in China.
  • Latitude and longitude swapped. Latitude comes first in most written positions, but not in every file format.
  • Minutes typed as decimals. 34° 30′ is 34.5°, not 34.30°. Latitude and Longitude Formats shows how to tell the formats apart and convert them.
  • ATAN in place of ATAN2. Plain arctangent cannot tell north-east from south-west. ATAN2 can.
  • A negative bearing left as it is. Add 360.

Frequently Asked Questions

What is the haversine formula?

A formula for the shortest distance between two points on a sphere, given their latitudes and longitudes. It is named for the haversine function, half of one minus the cosine of an angle, which keeps the arithmetic accurate when the two points are close together.

Is the haversine formula accurate enough?

For almost everything, yes. It is within about half a percent of the true distance at worst, and usually much closer: 142 m in 368 km on the Los Angeles to Las Vegas example. Surveying and long-range work use the ellipsoid.

How far is one degree of latitude or longitude?

A degree of latitude is about 111 km (69 miles) everywhere. A degree of longitude is about 111 km at the equator, 79 km at 45°, and 56 km at 60°.

Is the bearing true or magnetic?

True. Every formula here measures from true north. To follow the bearing with a compass, correct it for the declination where you are.

How do I get the bearing in the other direction?

Over a short distance, add or subtract 180°. Over a long one, work the formula again with the two points swapped, or read the bearing back from the calculator.

Does elevation change the distance?

Hardly. The formulas measure at sea level. At 3,000 m the same two positions are about 0.05% further apart. The climb between them is another matter, and is not part of a straight-line distance.

How do I measure the length of a route, not a straight line?

Add up the legs. The GPX Analyzer does that for a recorded track, and the Route Planner shows the length and bearing of every leg of a route as you draw it.

Related Tools and Guides