Two matches. A and B are within 800 m. C is in a covering cell, but its distance is 950 m.
What it is
Coordinates live on a surface. A sorted key lives on a line. A spatial index bridges the two by finding a manageable set of locations that could intersect a query. The final distance calculation tells you which candidates actually belong in the answer. Keeping those responsibilities separate makes boundary bugs easier to diagnose.
Why it exists
A geohash prefix describes a rectangle. A quadtree divides selected regions into four smaller regions. H3 gives locations hierarchical cell IDs and S2 orders cells along a space-filling curve. All can reduce search work, but no single cell ID means “everything within 800 metres of me.”
A Hilbert curve orders cells on each projected cube face.
Words to know
Covering
A set of regions whose union contains the query area.
Precision
The fraction of returned candidates that are true matches.
How it works
Step through the idea, or press Play.
Step 1 of 3
Cover the search area. Geohash encodes hierarchical rectangles. Scanning one prefix misses border neighbors. A quadtree splits busy regions recursively. H3 offers hierarchical cells for bucketing movement and aggregating demand.
Choose a key and resolution. S2 orders hierarchical cells along linked Hilbert curves on cube faces, giving useful locality in sorted keys. Nearby positions can still straddle key ranges. Finer cells reduce candidates but increase lookups.
Refine with distance. Build a covering that includes the entire radius, fetch candidates and check exact distance. Measure recall at boundaries, candidate count downtown and update churn for moving vehicles.
Understand it
Start here
Across the street, across the cell
Stand on the blue side of the street. Charger B is only a short walk away on the yellow side. Follow the expanding search area, then watch which pins survive the distance check.
Across the street, across the cell
Fetch intersecting cells and check candidates.
Structure
Radius lookup
Radius lookup
The index protects recall; exact filtering removes false positives.
A boundary test fixture
Place two chargers five metres apart on opposite sides of a cell edge, plus a third just beyond the requested distance but inside a covering cell. The first two must be returned and the third rejected. Repeat at high latitude and near the antimeridian.
A coarse cell might contain an entire dense neighborhood. Reads touch few keys yet measure many distances. Smaller cells reduce each bucket but expand the covering and cause moving vehicles to cross cell boundaries more often. Benchmark both downtown and rural routes. Record keys scanned, candidates fetched, accepted results and index updates per minute.
Crossing the date line
A viewport that goes from 179.9 degrees east to 179.9 degrees west is narrow on the globe. A naive minimum-to-maximum longitude range covers nearly the whole planet. Use geometry operations with the intended coordinate system, units and wraparound behavior. Test a driver just either side of the boundary and compare results with an exhaustive distance calculation on a small fixture.
Cover then refine
Fetch intersecting cells and check candidates.
Scan one prefix
A road can cross a cell edge within a few metres.
Summary
The whole topic in one minute.
Choose an index that cheaply overselects candidates. A complete covering protects recall; exact distance protects precision.