CHAPTER 13
Map clustering algorithms
Grid clustering, distance-based methods, and the Supercluster KD-tree algorithm Mapbox and MapLibre use to cluster millions of markers.
When you zoom out and 10,000 points overlap into a blob, you need clustering. Rendering 10,000 individual markers at zoom 5 provides no useful information — the entire city is covered by overlapping dots. Clustering groups nearby points into summary markers that convey density without visual chaos. Toggle the visualization to see the difference.
Grid clustering
Raw points
25
Clusters
11
Smaller cell → more clusters. Toggle to see the reduction.
Grid clustering
Grid clustering is the simplest form of spatial aggregation: divide the screen into cells, count points per cell. The same idea powers spreadsheet pivot tables and histogram binning.
Snap each point to a grid cell of size pixels at the current zoom level. All points in the same cell merge.
Fast, simple, but produces ugly square boundaries that shift as you pan. The cell boundaries are fixed to the pixel grid, not to the data, so panning even a few pixels causes points to jump between clusters.
Distance-based (DBSCAN-lite)
For each point, find all unclustered neighbors within radius r (in pixels). Form a cluster. Repeat. naively, but with a spatial index it becomes .
DBSCAN (Density-Based Spatial Clustering of Applications with Noise) is the full algorithm, which also identifies noise points that don't belong to any cluster. For map rendering, you typically skip the noise classification and cluster everything, which simplifies the implementation.
Supercluster algorithm (Mapbox)
The state of the art for web maps:
- Start with all points at max zoom.
- For each lower zoom level, cluster points within
radiuspixels using a KD-tree. - Each cluster has a representative position (weighted average) and a count.
- At render time, query the precomputed tree for the visible bbox at the current zoom.
This is what supercluster.js does. Pre-computation makes pan/zoom essentially free.
Continue reading "Map clustering algorithms"
You've reached the end of the free preview. Unlock all 22 paid chapters, including distance math, bearings, polygons, spatial indexing, and 3D map rendering — plus a downloadable PDF and the companion code repo.
- All 22 paid chapters with worked examples
- Downloadable PDF for offline reading
- Companion GitHub repo (JavaScript + Python)
- Free updates for life
Multiple payment options including Wise, PayPal, and bank transfer.
Related chapters
- Spatial indexing — Geohash, Quadtrees, S2, H3 — the data structure underneath clustering
- How maps render — tiles, vectors, and the GPU pipeline — where clustering happens in the pipeline
- Interpolation and heat maps — the visual cousin of clustering