Calculate distance between two latitude-longitude points? Haversine formula
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Calculating the distance between two points defined by latitude and longitude on the Earth's surface is a common problem in many fields, such as geolocation, mapping, and navigation. One widely-used formula to perform this calculation is the Haversine formula. In this article, we'll delve into the technical aspects of the Haversine formula, its applications, and provide examples to illustrate its use.
The Haversine Formula
The Haversine formula calculates the shortest distance over the earth’s surface, giving an "as-the-crow-flies" distance between two points. It's a solution to the spherical trigonometry problem and is defined by the following steps:
a = sin^2((Δphi) / 2) + cos(phi1) * cos(phi2) * sin^2((Δlambda) / 2)
c = 2 * atan2(sqrt(a), sqrt(1 - a))
d = R * c
Where:
phi1andphi2are the latitudes of point 1 and point 2 in radians.Δphiis the difference between the latitudes of the two points (that is,phi2 - phi1).Δlambdais the difference between the longitudes of the two points (that is,lambda2 - lambda1).Ris the Earth's radius (mean radius = 6,371 km or 3,959 miles).dis the distance between the two points along the surface of the sphere.
Examples and Application
Below is an example implementation of the Haversine formula in Python:
Output:Distance: 840.3 km
Key Considerations
- Radius of the Earth (
R): The choice of Earth's radius is crucial. While 6,371 km is commonly used as an average radius, for higher accuracy over small distances, local radii may vary. - Report Outputs: The formula may yield results slightly different from actual path distances because the Earth isn't a perfect sphere but an oblate spheroid. Corrections for the Earth's ellipsoidal shape can be considered for more precision critical scenarios.
- Spherical Violations: For distances over large regions (>1000 km), the curvature of the Earth and the Earth's actual shape (geoid) may cause discrepancies that the simple Haversine does not account for.
Summary Table
| Concept | Description |
| Haversine Formula | Method to calculate the great-circle distance between two points using latitude-longitude. |
| Earth's Radius | 6,371 km 3,959 miles |
| Input Requirement | Latitude & longitude in decimal degrees compatibility |
| Output | Distance in kilometers (or miles) |
| Precision Note | More accurate for short distances; Errors may increase with larger distances. |
Additional Considerations
Vincenty Formulae
For more precise distance measurements, especially over long distances, geodesic calculations such as Vincenty's formulae can be used. These take into account more complexities regarding the shape of the Earth but are computationally more intensive.
Practical Applications
The Haversine formula is widely used in:
- GPS Technology: For determining the straight-line distance between two geographic points.
- Aviation: Flight path calculations between airports.
- Mobile Network Triangulation: Estimating locations based on distances from known coordinate points.
By understanding and implementing the Haversine formula, one can efficiently determine distances using geographic coordinates, contributing to numerous practical applications in today's geospatially aware world.

