Haversine Formula
Calculate the distance between two points on Earth using latitude and longitude.
The standard GPS distance formula.
The Formula
d = 2R × arcsin(√a)
The Haversine formula calculates the shortest distance between two points on a sphere using their latitude and longitude. It accounts for the curvature of the Earth and is accurate for most practical purposes.
Variables
| Symbol | Meaning |
|---|---|
| d | Distance between the two points (km or miles) |
| R | Radius of Earth (6,371 km or 3,959 miles) |
| φ₁, φ₂ | Latitude of point 1 and point 2 (in radians) |
| λ₁, λ₂ | Longitude of point 1 and point 2 (in radians) |
| Δφ | Difference in latitude (φ₂ - φ₁) |
| Δλ | Difference in longitude (λ₂ - λ₁) |
Example 1
Distance from New York (40.7128°N, 74.0060°W) to London (51.5074°N, 0.1278°W)
Convert to radians: φ₁ = 0.7106, φ₂ = 0.8989, λ₁ = -1.2918, λ₂ = -0.00223
Δφ = 0.1883, Δλ = 1.2896
a = sin²(0.0942) + cos(0.7106) × cos(0.8989) × sin²(0.6448)
a = 0.00886 + 0.7602 × 0.6270 × 0.3620 = 0.1814
d = 2 × 6371 × arcsin(√0.1814) ≈ 5,570 km
Example 2
Distance from Tokyo (35.6762°N, 139.6503°E) to Sydney (33.8688°S, 151.2093°E)
Δφ = 69.545° = 1.2137 rad, Δλ = 11.559° = 0.2018 rad
Computing the haversine components and summing:
d ≈ 7,823 km
When to Use It
Use the Haversine formula when:
- Calculating distances between GPS coordinates
- Building location-based apps and services
- Planning flight routes or shipping distances
- Finding the nearest point of interest from a given location