Simple calculations for working with lat/lon and km distance?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Latitude and longitude are angular coordinates, so any distance calculation has to convert angles into physical units on the Earth's surface. For rough work, simple approximations are often enough, but for route matching, search radii, or analytics across larger distances, it is better to use a spherical formula such as Haversine.
A quick mental model for degrees and kilometers
A useful starting point is that one degree of latitude is about 111.32 kilometers almost everywhere on Earth. Longitude is different because east-west spacing shrinks as you move toward the poles.
A rough conversion for longitude is:
- kilometers per degree longitude =
111.32 * cos(latitude)
That means a one-degree longitude change near the equator is about 111 km, but at 60 degrees latitude it is only about half that.
Using the Haversine formula
For most programming tasks, the Haversine formula is the standard simple answer. It estimates the great-circle distance between two points on a sphere.
This returns roughly the distance between New York City and Los Angeles in kilometers.
A simpler approximation for short distances
If the points are close together, a flat-Earth approximation is usually fine and slightly cheaper to compute. One common choice is the equirectangular approximation:
For local searches, clustering, and viewport calculations, this is often accurate enough.
Estimating a bounding box around a point
A common geospatial task is: "find all points within 5 km." Before you run an exact distance formula, you can build a rough latitude-longitude box to reduce the candidate set.
This does not replace the final distance check, but it is a practical prefilter.
When accuracy matters more
The Earth is not a perfect sphere, so very accurate surveying uses ellipsoidal formulas instead of Haversine. But for most application development, including maps, search radii, and telemetry dashboards, Haversine is more than sufficient.
The important thing is choosing the right level of precision for the task. A delivery app, for example, can tolerate small approximations for nearby points, while legal land measurement cannot.
Common Pitfalls
The most common mistake is forgetting to convert degrees to radians before using trigonometric functions. sin() and cos() expect radians, not degrees.
Another issue is treating latitude and longitude changes as if they always map to the same number of kilometers. Latitude spacing is fairly stable, but longitude spacing changes with latitude.
Be careful with short-distance approximations near the poles or across long distances. The farther apart the points are, the more you need a proper spherical formula.
Finally, remember that straight-line surface distance is not the same as travel distance. Haversine gives a geographic distance, not the road distance or walking time.
Summary
- One degree of latitude is roughly
111.32km. - Longitude distance per degree depends on latitude and shrinks toward the poles.
- Use the Haversine formula for general-purpose point-to-point distance.
- Use a flat approximation only for nearby points when you need speed more than precision.
- For search radii, a bounding box is a useful prefilter before exact distance checks.

