Enter the latitude and longitude of two points to get the distance between them along the Earth's surface in km and miles. You can switch the input between decimal degrees and DMS.
Table of Contents
-
What you can do on this page
-
What is this calculation used for?
-
How to Use
-
Formula
-
Symbols and terms
-
Good to know before you start
-
How to calculate it in Excel
-
How to calculate it in Google Sheets
-
How to calculate it in Python
-
How to write it in LaTeX and other math languages (copy and paste)
-
How to have ChatGPT do the calculation
-
DataChef Features
-
Related Features
-
NumberChef Calculators List
What you can do on this page
- Enter the latitude and longitude of two points, and you get the distance between them along the Earth's surface (in miles and km) on the spot
- You can enter decimal degrees (for example, 40.7128) or degrees, minutes and seconds (DMS, for example, 40°42′46.08″). Switch with the radio buttons
- The distance is calculated with Lambert's formula, which treats the Earth as an ellipsoid (a slightly flattened sphere). This is closer to the real Earth than the haversine formula, which treats the Earth as a perfect sphere
- A plain-language explanation of the formulas and copy-and-paste formulas for Excel, Google Sheets and Python are also on this page
What is this calculation used for?
Airplanes fly close to the shortest path along the curve of the Earth (the great-circle route), so the flight distance between cities is estimated as a distance along the Earth's surface, like the one on this page (New York to London is about 3,470 miles, or 5,585 km). Airline mileage charts are based on this kind of distance too.
On a flat map, flights from the US to Asia seem to take a detour over Alaska and near the Arctic. Calculating this distance shows why that path is actually the shortest.
When a map or delivery app sorts places by "nearest first", it calculates the distance between your location and each store from their latitudes and longitudes. In programming, the haversine formula (sphere model) is the standard tool for this, and ellipsoid formulas like Lambert's formula on this page are used when more accuracy is needed.
Behind the apps you use every day, this kind of formula is calculated over and over.
A GPS watch or running app records your position every few seconds, calculates the distance between each pair of points with this kind of formula, and adds them up. Each step is only tens of feet, but the latitude-and-longitude-to-distance conversion is repeated thousands of times to show "you ran 6.2 miles today".
At sea, distance is measured in nautical miles (1 nautical mile = 1.852 km, about 1.15 miles, roughly the length of 1 minute of latitude). The route from the departure port to the destination is planned as a distance along the curve of the Earth, and arrival times and fuel estimates start from this distance calculation.
Shipping carries much of the world's trade, and latitude and longitude distances are part of what keeps it running.
Earthquake reports such as "the epicenter was 12 miles southwest of the city" come from the same kind of calculation: the distance between the latitude and longitude of the epicenter (the point on the surface directly above the quake) and those of each town or monitoring station.
Knowing in numbers how far your home is from active faults and past epicenters is a good first step in preparing for earthquakes.
Formula
Symbols and terms
Symbols
| \(\varphi\) | phi | Latitude: the angle that tells how far north or south of the equator (0°) a place is. North is positive and south is negative (−90° to 90°). |
| \(\lambda\) | lambda | Longitude: the angle that gives the east-west position, measured from the prime meridian (0°) through the old Royal Observatory in Greenwich, England. East is positive and west is negative (−180° to 180°). |
| \(f\) | eff | The flattening of the Earth: how squashed the ellipsoid is, as a ratio. This calculation uses \(f = \dfrac{1}{298.257}\). |
| \(U\) | you | The reduced latitude: the latitude on the ellipsoid converted with \(U = \arctan((1-f)\tan\varphi)\) so that it can be used in formulas for a sphere. |
| \(c\) | see | The central angle: the angle between the two points as seen from the center of the Earth (in radians). Through "arc length = radius × central angle", it is the base of the distance. |
| \(X\), \(Y\) | ex, why | In-between values used to correct for the flattened shape. They are calculated from the central angle \(c\), the average of the reduced latitudes \(P = \dfrac{U_1+U_2}{2}\) and half their difference \(Q = \dfrac{U_2-U_1}{2}\). |
| \(d\) | dee | The distance between the two points, from the first letter of "distance". This page shows it in miles and km (kilometers). |
| ° ′ ″ | degree, minute, second | The symbols for degrees, minutes and seconds of angle. 1° = 60′ and 1′ = 60″. Example - 40°42′46.08″ (40 degrees, 42 minutes, 46.08 seconds). |
Terms
| latitude | The angle that gives the north-south position, with the equator at 0°. The North Pole is 90° N and the South Pole is 90° S. It is a basic idea of maps and location data, taught in elementary and middle school geography. |
| longitude | The angle that gives the east-west position, with the prime meridian (the line through Greenwich, England) at 0°. It goes up to 180° east and 180° west. The contiguous US lies between about 67° W and 125° W. |
| DMS | A way of writing angles in base 60 with degrees, minutes and seconds. 1 degree = 60 minutes and 1 minute = 60 seconds. It is common on USGS topographic maps, nautical charts and GPS devices. |
| ellipsoid | Here, an ellipsoid of revolution - the solid you get by spinning an ellipse around its axis. The Earth's rotation makes it bulge at the equator, so it is modeled as an ellipsoid rather than a sphere. |
| flattening | The ratio that shows how squashed an ellipsoid is, defined as (equatorial radius − polar radius) ÷ equatorial radius. For the Earth it is about 1/300. |
| reduced latitude | The latitude of a point on the ellipsoid after it is moved onto the helper circle (sphere) the ellipse is based on. It is found with \(U = \arctan((1-f)\tan\varphi)\) and is used in geodesy (the science of measuring the Earth's shape). It is also called the parametric latitude. |
| central angle | The angle between two points as seen from the center of a circle or sphere. The sector rule "arc length = radius × central angle (in radians)" is the base of the distance calculation on this page. |
| geodesic | The shortest path between two points on a curved surface. On the Earth it is close to the great-circle route, and airline routes follow roughly this shape. This page finds the approximate length along the geodesic. |
| haversine formula | A well-known formula for the distance between two points that treats the Earth as a perfect sphere. Lambert's formula on this page is an improved version for the ellipsoid, and it uses a haversine-type formula for the central angle in the first step. |
| Lambert's formula | A method for the distance between two points that models the Earth as an ellipsoid. Its error is said to be about 10 meters (about 30 feet) even over thousands of miles, which is more accurate than treating the Earth as a sphere. |
| mile | A unit of distance used mainly in the US and the UK (the statute mile). 1 mile = 5,280 feet ≈ 1.609 km (exactly 1.609344 km by the international definition). This calculator converts with 1 mile = 1.60935 km. It is a different unit from the nautical mile below. |
| nautical mile | A unit of distance used at sea and in aviation. 1 nautical mile = 1.852 km (about 1.15 miles or 6,076 feet). It was chosen to be about the length of 1 minute (1/60 of a degree) of latitude. |
Good to know before you start
Here is what helps you use the calculation on this page with real understanding, not just by pressing the button.
To just find a distance, knowing how to read latitude and longitude is enough. To understand what is inside the formulas, go back as far as trigonometric ratios and radians.
| Latitude and longitude (Grades 4–6 social studies) |
|
| Angles and base 60 (Grades 3–4 time, Grade 4 angles) |
|
| Arc length of a sector (Geometry) |
|
| Trigonometric ratios sin, cos and tan (Geometry and Algebra 2) |
|
| Radians (Algebra 2 and Precalculus) |
|
| The shape of the Earth (middle school Earth science) |
|
How to calculate it in Excel
| Degrees | 38 |
| Minutes | 53 |
| Seconds | 51.36 |
| Decimal degrees (add − for S and W) | =B1+B2/60+B3/3600 |
| Point 1 latitude (decimal; S is −) | 40.7128 |
| Point 1 longitude (decimal; W is −) | -74.0060 |
| Point 2 latitude | 38.8976 |
| Point 2 longitude | -77.0366 |
| Flattening f | =1/298.257 |
| Reduced latitude U1 | =ATAN((1-B5)*TAN(RADIANS(B1))) |
| Reduced latitude U2 | =ATAN((1-B5)*TAN(RADIANS(B3))) |
| Longitude difference Δλ (radians) | =RADIANS(B4-B2) |
| Haversine term a | =SIN((B7-B6)/2)^2+COS(B6)*COS(B7)*SIN(B8/2)^2 |
| Central angle c (radians) | =2*ATAN2(SQRT(1-B9),SQRT(B9)) |
| Average P | =(B6+B7)/2 |
| Half difference Q | =(B7-B6)/2 |
| Correction value X | =(B10-SIN(B10))*SIN(B11)^2*COS(B12)^2/COS(B10/2)^2 |
| Correction value Y | =(B10+SIN(B10))*SIN(B12)^2*COS(B11)^2/SIN(B10/2)^2 |
| Distance (miles) | =6378.1/1.60935*(B10-B5*(B13+B14)/2) |
| Distance (km) | =B15*1.60935 |
The second table is every step of Lambert's formula. B1 to B4 are the inputs (decimal degrees), B6 onward are the in-between values of formulas 1 and 2, B15 is the distance in miles (about 204.15), and B16 is the distance in km (about 328.56). The calculator on this page rounds these to 204.2 mi and 328.6 km.
RADIANS(…) converts degrees to radians. In Excel, ATAN2(x, y) takes its arguments in the order (x-coordinate, y-coordinate), so note that c is written as 2×ATAN2(√(1−a), √a). 6378.1/1.60935 is the equatorial radius in miles (about 3963.15).
How to calculate it in Google Sheets
| Degrees | 38 |
| Minutes | 53 |
| Seconds | 51.36 |
| Decimal degrees (add − for S and W) | =B1+B2/60+B3/3600 |
| Point 1 latitude (decimal; S is −) | 40.7128 |
| Point 1 longitude (decimal; W is −) | -74.0060 |
| Point 2 latitude | 38.8976 |
| Point 2 longitude | -77.0366 |
| Flattening f | =1/298.257 |
| Reduced latitude U1 | =ATAN((1-B5)*TAN(RADIANS(B1))) |
| Reduced latitude U2 | =ATAN((1-B5)*TAN(RADIANS(B3))) |
| Longitude difference Δλ (radians) | =RADIANS(B4-B2) |
| Haversine term a | =SIN((B7-B6)/2)^2+COS(B6)*COS(B7)*SIN(B8/2)^2 |
| Central angle c (radians) | =2*ATAN2(SQRT(1-B9),SQRT(B9)) |
| Average P | =(B6+B7)/2 |
| Half difference Q | =(B7-B6)/2 |
| Correction value X | =(B10-SIN(B10))*SIN(B11)^2*COS(B12)^2/COS(B10/2)^2 |
| Correction value Y | =(B10+SIN(B10))*SIN(B12)^2*COS(B11)^2/SIN(B10/2)^2 |
| Distance (miles) | =6378.1/1.60935*(B10-B5*(B13+B14)/2) |
| Distance (km) | =B15*1.60935 |
How to calculate it in Python
import math
# Latitude and longitude of the two points (decimal degrees; south and west are negative)
lat1, lon1 = 40.7128, -74.0060 # point 1 (near New York City Hall)
lat2, lon2 = 38.8976, -77.0366 # point 2 (near the White House, Washington, D.C.)
flattening = 1 / 298.257 # flattening of the Earth, f
equator_radius_km = 6378.1 # equatorial radius in km
equator_radius_mi = equator_radius_km / 1.60935 # equatorial radius in miles (about 3963.15)
# Formula 1, first half: reduced latitudes (latitudes shrunk by the flattening)
u1 = math.atan((1 - flattening) * math.tan(math.radians(lat1)))
u2 = math.atan((1 - flattening) * math.tan(math.radians(lat2)))
delta_lon = math.radians(lon2 - lon1)
# Formula 1, second half: central angle c between the two points (haversine-type formula)
a = math.sin((u2 - u1) / 2) ** 2 + math.cos(u1) * math.cos(u2) * math.sin(delta_lon / 2) ** 2
central_angle = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
# Formula 2: correction for the flattened shape (X and Y), then the distance
p = (u1 + u2) / 2
q = (u2 - u1) / 2
x = (central_angle - math.sin(central_angle)) * math.sin(p) ** 2 * math.cos(q) ** 2 / math.cos(central_angle / 2) ** 2
y = (central_angle + math.sin(central_angle)) * math.sin(q) ** 2 * math.cos(p) ** 2 / math.sin(central_angle / 2) ** 2
distance_mi = equator_radius_mi * (central_angle - flattening * (x + y) / 2)
distance_km = distance_mi * 1.60935
print(f"Distance between the two points: {distance_mi:.1f} miles ({distance_km:.1f} km)")
How to write it in LaTeX and other math languages (copy and paste)
U₁ = atan((1 − f)·tanφ₁), U₂ = atan((1 − f)·tanφ₂), c = 2·asin(√(sin²((U₂ − U₁)/2) + cosU₁·cosU₂·sin²((λ₂ − λ₁)/2)))
U_1 = \arctan\bigl((1-f)\tan\varphi_1\bigr),\ U_2 = \arctan\bigl((1-f)\tan\varphi_2\bigr),\quad c = 2\arcsin\sqrt{\sin^2\frac{U_2-U_1}{2} + \cos U_1 \cos U_2 \sin^2\frac{\lambda_2-\lambda_1}{2}}
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block">
<mrow>
<msub><mi>U</mi><mi>i</mi></msub>
<mo>=</mo>
<mi>arctan</mi>
<mo>⁡</mo>
<mrow>
<mo>(</mo>
<mrow><mo>(</mo><mn>1</mn><mo>−</mo><mi>f</mi><mo>)</mo></mrow>
<mi>tan</mi><mo>⁡</mo><msub><mi>φ</mi><mi>i</mi></msub>
<mo>)</mo>
</mrow>
<mo>,</mo>
<mspace width="1em"/>
<mi>c</mi>
<mo>=</mo>
<mn>2</mn>
<mi>arcsin</mi>
<mo>⁡</mo>
<msqrt>
<mrow>
<msup><mi>sin</mi><mn>2</mn></msup>
<mo>⁡</mo>
<mfrac>
<mrow><msub><mi>U</mi><mn>2</mn></msub><mo>−</mo><msub><mi>U</mi><mn>1</mn></msub></mrow>
<mn>2</mn>
</mfrac>
<mo>+</mo>
<mi>cos</mi><mo>⁡</mo><msub><mi>U</mi><mn>1</mn></msub>
<mi>cos</mi><mo>⁡</mo><msub><mi>U</mi><mn>2</mn></msub>
<msup><mi>sin</mi><mn>2</mn></msup>
<mo>⁡</mo>
<mfrac>
<mrow><msub><mi>λ</mi><mn>2</mn></msub><mo>−</mo><msub><mi>λ</mi><mn>1</mn></msub></mrow>
<mn>2</mn>
</mfrac>
</mrow>
</msqrt>
</mrow>
</math>
U_1 = arctan((1-f) tan(varphi_1)), U_2 = arctan((1-f) tan(varphi_2)), c = 2 arcsin(sqrt(sin^2((U_2-U_1)/2) + cos(U_1) cos(U_2) sin^2((lambda_2-lambda_1)/2)))
u1 = ArcTan[(1 - f) Tan[phi1]]; u2 = ArcTan[(1 - f) Tan[phi2]]; c = 2 ArcSin[Sqrt[Sin[(u2 - u1)/2]^2 + Cos[u1] Cos[u2] Sin[(lambda2 - lambda1)/2]^2]]
U1 := arctan((1 - f)*tan(phi1)); U2 := arctan((1 - f)*tan(phi2)); c := 2*arcsin(sqrt(sin((U2 - U1)/2)^2 + cos(U1)*cos(U2)*sin((lambda2 - lambda1)/2)^2));
u1 = atan((1 - f)*tan(phi1)); u2 = atan((1 - f)*tan(phi2)); c = 2*asin(sqrt(sin((u2 - u1)/2)^2 + cos(u1)*cos(u2)*sin((lambda2 - lambda1)/2)^2));
U_1 = atan((1 - f) tan(φ_1)), U_2 = atan((1 - f) tan(φ_2)), c = 2 asin(√(sin^2((U_2 - U_1)/2) + cos(U_1) cos(U_2) sin^2((λ_2 - λ_1)/2)))
d = 3963.15·(c − f·(X + Y)/2), X = (c − sin c)·sin²P·cos²Q ÷ cos²(c/2), Y = (c + sin c)·sin²Q·cos²P ÷ sin²(c/2), P = (U₁ + U₂)/2, Q = (U₂ − U₁)/2
d = 3963.15\left(c - \frac{f(X + Y)}{2}\right),\quad X = \frac{(c - \sin c)\sin^2 P \cos^2 Q}{\cos^2(c/2)},\quad Y = \frac{(c + \sin c)\sin^2 Q \cos^2 P}{\sin^2(c/2)},\quad P = \frac{U_1 + U_2}{2},\ Q = \frac{U_2 - U_1}{2}
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block">
<mrow>
<mi>d</mi>
<mo>=</mo>
<mn>3963.15</mn>
<mrow>
<mo>(</mo>
<mi>c</mi>
<mo>−</mo>
<mfrac>
<mrow><mi>f</mi><mo>(</mo><mi>X</mi><mo>+</mo><mi>Y</mi><mo>)</mo></mrow>
<mn>2</mn>
</mfrac>
<mo>)</mo>
</mrow>
<mo>,</mo>
<mspace width="1em"/>
<mi>X</mi>
<mo>=</mo>
<mfrac>
<mrow>
<mo>(</mo><mi>c</mi><mo>−</mo><mi>sin</mi><mo>⁡</mo><mi>c</mi><mo>)</mo>
<msup><mi>sin</mi><mn>2</mn></msup><mo>⁡</mo><mi>P</mi>
<msup><mi>cos</mi><mn>2</mn></msup><mo>⁡</mo><mi>Q</mi>
</mrow>
<mrow><msup><mi>cos</mi><mn>2</mn></msup><mo>⁡</mo><mrow><mo>(</mo><mi>c</mi><mo>/</mo><mn>2</mn><mo>)</mo></mrow></mrow>
</mfrac>
<mo>,</mo>
<mspace width="1em"/>
<mi>Y</mi>
<mo>=</mo>
<mfrac>
<mrow>
<mo>(</mo><mi>c</mi><mo>+</mo><mi>sin</mi><mo>⁡</mo><mi>c</mi><mo>)</mo>
<msup><mi>sin</mi><mn>2</mn></msup><mo>⁡</mo><mi>Q</mi>
<msup><mi>cos</mi><mn>2</mn></msup><mo>⁡</mo><mi>P</mi>
</mrow>
<mrow><msup><mi>sin</mi><mn>2</mn></msup><mo>⁡</mo><mrow><mo>(</mo><mi>c</mi><mo>/</mo><mn>2</mn><mo>)</mo></mrow></mrow>
</mfrac>
<mo>,</mo>
<mspace width="1em"/>
<mi>P</mi>
<mo>=</mo>
<mfrac><mrow><msub><mi>U</mi><mn>1</mn></msub><mo>+</mo><msub><mi>U</mi><mn>2</mn></msub></mrow><mn>2</mn></mfrac>
<mo>,</mo>
<mi>Q</mi>
<mo>=</mo>
<mfrac><mrow><msub><mi>U</mi><mn>2</mn></msub><mo>−</mo><msub><mi>U</mi><mn>1</mn></msub></mrow><mn>2</mn></mfrac>
</mrow>
</math>
d = 3963.15 (c - f(X + Y)/2), X = ((c - sin c) sin^2(P) cos^2(Q))/(cos^2(c/2)), Y = ((c + sin c) sin^2(Q) cos^2(P))/(sin^2(c/2)), P = (U_1 + U_2)/2, Q = (U_2 - U_1)/2
p = (u1 + u2)/2; q = (u2 - u1)/2; x = (c - Sin[c]) Sin[p]^2 Cos[q]^2/Cos[c/2]^2; y = (c + Sin[c]) Sin[q]^2 Cos[p]^2/Sin[c/2]^2; d = 3963.15 (c - f (x + y)/2)
P := (U1 + U2)/2; Q := (U2 - U1)/2; X := (c - sin(c))*sin(P)^2*cos(Q)^2/cos(c/2)^2; Y := (c + sin(c))*sin(Q)^2*cos(P)^2/sin(c/2)^2; d := 3963.15*(c - f*(X + Y)/2);
p = (u1 + u2)/2; q = (u2 - u1)/2; x = (c - sin(c))*sin(p)^2*cos(q)^2/cos(c/2)^2; y = (c + sin(c))*sin(q)^2*cos(p)^2/sin(c/2)^2; d = 3963.15*(c - f*(x + y)/2);
d = 3963.15(c - f(X + Y)/2), X = (c - sin c) sin^2(P) cos^2(Q)/cos^2(c/2), Y = (c + sin c) sin^2(Q) cos^2(P)/sin^2(c/2), P = (U_1 + U_2)/2, Q = (U_2 - U_1)/2
D = d + m/60 + s/3600
D = d + \frac{m}{60} + \frac{s}{3600}
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block">
<mrow>
<mi>D</mi>
<mo>=</mo>
<mi>d</mi>
<mo>+</mo>
<mfrac><mi>m</mi><mn>60</mn></mfrac>
<mo>+</mo>
<mfrac><mi>s</mi><mn>3600</mn></mfrac>
</mrow>
</math>
D = d + m/60 + s/3600
d + m/60 + s/3600
deg := d + m/60 + s/3600; # D (the differential operator) is reserved in Maple, so the decimal degrees are named deg
D = d + m/60 + s/3600;
D = d + m/60 + s/3600
How to have ChatGPT do the calculation
You are a geography calculation assistant. Do the following calculation by actually running Python code, and base your answer only on the numbers from the execution result (do not answer by mental math or guessing). Find the distance between two points from their latitudes and longitudes using Lambert's formula, which models the Earth as an ellipsoid with flattening f = 1/298.257. Steps: convert each latitude to the reduced latitude U = atan((1-f)·tanφ); a = sin²((U2-U1)/2) + cosU1·cosU2·sin²((λ2-λ1)/2); central angle c = 2·atan2(√a, √(1-a)); P = (U1+U2)/2; Q = (U2-U1)/2; X = (c-sin c)·sin²P·cos²Q/cos²(c/2); Y = (c+sin c)·sin²Q·cos²P/sin²(c/2); distance (km) = 6378.1·(c - f·(X+Y)/2). Point 1: 40.7128° N, 74.0060° W (near New York City Hall) Point 2: 38.8976° N, 77.0366° W (near the White House, Washington, D.C.) Give the distance in miles and km (1 mile = 1.60935 km), and show the code you used and the numbers from the execution result.
How to Use
-
1Enter your numbersType the numbers you want to calculate with into the input fields
-
2CalculatePress the "Calculate" button
-
3Check the resultThe result appears on the spot. The same page also explains the idea behind the calculation and the formula
DataChef Features
No technical knowledge required.
Intuitive and user-friendly operation.
Can be used without registering personal information.
Automatic file deletion by clicking "download".
and rapid file conversion.
No attribution required.
No need to contact us for commercial use permission.
