Algorithm to find all Latitude Longitude locations within a certain distance from a given Lat Lng location
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Finding all latitude-longitude points within a given radius is a classic geospatial filtering problem. The standard practical solution is two-stage: first reduce the candidate set with a bounding box, then compute an accurate spherical distance such as Haversine only for the remaining candidates.
Why You Usually Need Two Steps
If you compare the target point to every stored point with a full spherical distance formula, the query becomes expensive as the dataset grows. A bounding box makes the search much cheaper by eliminating points that are obviously too far away.
So the usual algorithm is:
- compute a bounding box around the target point
- keep only points inside that box
- run an exact distance formula on the survivors
- return those within the requested radius
This scales much better than doing exact great-circle math against every row.
Haversine for Accurate Distance
The Haversine formula computes great-circle distance on a sphere and is accurate enough for many applications.
This gives you the exact filter after the candidate set has already been reduced.
Compute a Bounding Box First
For a target point and radius, you can estimate latitude and longitude limits that form a search rectangle.
This approximation is not the final answer, but it is very effective for prefiltering.
Full Example on an In-Memory List
Here is a complete Python example that combines both stages.
That pattern is perfectly fine for small or medium in-memory datasets.
Database Queries Need Spatial Indexes
For large datasets, the algorithm should move into the database or spatial engine rather than loading all points into application memory.
Common choices include:
- PostGIS for PostgreSQL
- MySQL spatial features
- dedicated search engines with geo queries
Even in a database, the same logic applies conceptually: use an index-friendly prefilter first, then apply exact distance checks.
For example, a SQL query often starts with latitude and longitude range filtering before applying a distance function.
Edge Cases You Should Not Ignore
There are two famous edge cases:
- points near the poles
- points near the antimeridian around
+180and-180longitude
A naive longitude range can break when the bounding box crosses the antimeridian. In that case, the longitude filter must wrap correctly instead of assuming a simple min <= lon <= max check.
For small city-scale searches, many systems never hit this edge case. For global systems, you absolutely need to handle it.
When Euclidean Distance Is Good Enough
If all points are within a very small local region and the application does not need geographic precision, projecting coordinates into a local plane and using Euclidean distance may be acceptable.
But once the search area grows or the application spans wider geography, use spherical or ellipsoidal formulas. Do not pretend latitude and longitude are ordinary Cartesian coordinates at all scales.
Common Pitfalls
A common mistake is running Haversine against every row without any prefiltering. That becomes unnecessarily expensive on large datasets.
Another issue is using a bounding box as if it were the final answer. A box includes corner points that may still be outside the actual radius.
Developers also often forget antimeridian and polar edge cases when they generalize a local solution into a global one.
Finally, if the dataset is large, do not pull everything into memory. Use a spatial index where the data lives.
Summary
- Use a bounding box to cut down the candidate set quickly.
- Use Haversine or another geodesic formula for the exact distance check.
- For large datasets, move the query into a spatially indexed database.
- Handle antimeridian and polar edge cases if the system is global.
- Bounding-box plus exact-distance filtering is the standard practical solution.

