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.
Introduction
When you need the straight-line surface distance between two latitude-longitude points, the Haversine formula is a standard starting point. It models Earth as a sphere and gives a good approximation for many mapping, navigation, and geofencing tasks.
What the Haversine Formula Computes
Latitude and longitude are angular coordinates, so you cannot subtract them directly and call the result a distance. The Haversine formula converts the angular separation into an arc distance along the Earth’s surface.
The inputs are:
- latitude and longitude of point one
- latitude and longitude of point two
- an Earth radius in the distance unit you want, usually kilometers or meters
The output is the great-circle distance between the two positions.
Python Implementation
A compact Python implementation is often the easiest way to understand the formula.
The important step is converting degrees to radians before using trigonometric functions.
Why Radians Matter
Trigonometric functions in standard programming libraries expect radians, not degrees. Forgetting the conversion is one of the fastest ways to get a wildly wrong answer.
You can also return meters instead of kilometers by changing the Earth radius constant.
That is often more convenient for proximity checks and small-radius geofence logic.
Use Cases Where Haversine Is Good Enough
The Haversine formula is a strong default for:
- map features that show approximate distance between cities
- store or driver proximity checks
- clustering or filtering points by range
- general geospatial analytics where meter-level geodetic precision is not required
For many applications, the approximation error from treating Earth as a sphere is small enough to ignore.
Vectorized Example With NumPy
If you need distances from one reference point to many coordinates, vectorizing the calculation can be much faster than looping in Python.
This is useful in recommendation, routing preparation, or analytics workflows that evaluate many candidate locations.
Know the Limits of the Formula
Haversine assumes a spherical Earth. For high-precision surveying, aviation-grade routing, or long-distance geodesic calculations where ellipsoidal accuracy matters, a more precise geodesic method may be better.
That does not make Haversine wrong. It just means you should match the method to the precision requirements of the task.
Another practical issue is that Haversine computes a surface distance, not driving distance or travel time. For routing applications, it is only a geometric baseline.
Common Pitfalls
A common mistake is forgetting to convert degrees to radians. Another is mixing kilometers and meters by changing the radius constant in one place and the expected output unit in another.
It is also easy to treat Haversine distance as route distance, which can be very misleading in road networks or mountainous terrain. Finally, for extremely small local distances, people sometimes use overcomplicated geodesic tooling when Haversine would have been more than adequate.
Summary
- Use the Haversine formula to estimate great-circle distance between two latitude-longitude points.
- Convert degrees to radians before applying trigonometric functions.
- Choose an Earth radius constant that matches your desired output unit.
- Haversine is usually good enough for mapping, geofencing, and general spatial filtering.
- It estimates surface distance, not route distance, and it is approximate rather than geodetically exact.

