Leaflet.markercluster
Beautiful, animated marker clustering for Leaflet maps with thousands of points
Repository Health
Technical Analysis
Leaflet.markercluster is the de facto standard plugin for grouping large numbers of point markers into interactive clusters on a Leaflet map. Instead of rendering thousands of individual markers (which crushes browser performance and turns the map into visual noise), it groups nearby markers into a single cluster icon that shows a count, expands with a smooth animation as you zoom in, and “spiderfies” into a fan of individual pins once markers can no longer be usefully separated.
The library plugs directly into Leaflet’s existing layer model — L.markerClusterGroup() behaves like any other L.LayerGroup, so existing addLayer/removeLayer code keeps working. It ships with chunked bulk-loading for adding tens of thousands of markers without freezing the page, a distance-grid spatial index for fast neighbor lookups, and a QuickHull-based convex-hull calculation for the “show coverage on hover” bounds outline.
What You Get
- A drop-in
MarkerClusterGrouplayer that behaves like a standard LeafletLayerGroup(addLayer,removeLayer,addLayers,clearLayers) - Animated cluster-to-marker transitions on zoom, with a spiderfy mode that fans out overlapping markers at max zoom instead of stacking them invisibly
- Chunked bulk loading (
chunkedLoading) so adding tens of thousands of markers doesn’t block the browser’s main thread - A spatial distance-grid index (
DistanceGrid) for fast add/remove/lookup of markers by pixel proximity - A QuickHull convex-hull implementation used to draw the coverage polygon shown when hovering a cluster
- Customizable cluster icons via
iconCreateFunction, plus options for coverage-on-hover, zoom-to-bounds-on-click, and per-cluster-pane rendering
Common Use Cases
- Plotting store locators, real-estate listings, or IoT device fleets with thousands of points on a single Leaflet map
- Visualizing dense geospatial datasets (crime reports, sensor readings, event check-ins) without overwhelming the viewport
- Building dashboards that need to bulk-load large marker sets on load and keep panning/zooming smooth
- Adding a polished, animated cluster interaction (expand-on-zoom, spiderfy-on-max-zoom) to an existing Leaflet-based mapping UI
Under The Hood
Architecture: The plugin is organized as a set of files under src/ that each extend Leaflet’s own prototype-based class system via L.Class.extend/L.FeatureGroup.extend. MarkerClusterGroup.js (the core, ~1400 lines) owns the public API and orchestrates three collaborators: DistanceGrid.js maintains a bucketed spatial index keyed by pixel coordinates for O(1)-ish neighbor lookups when deciding which markers to merge into a cluster; MarkerCluster.js models an individual cluster node (itself a marker with getChildCount()/getAllChildMarkers()/bounds) and composes a tree of clusters per zoom level; MarkerCluster.Spiderfier.js (ported from OverlappingMarkerSpiderfier-Leaflet) handles fanning overlapping markers into a circle or spiral at maximum zoom; and MarkerCluster.QuickHull.js computes the convex hull shown as the coverage polygon on hover. MarkerClusterGroup.Refresh.js adds a refreshClusters() extension for updating icons when marker data changes without a full re-cluster. src/index.js is a thin barrel that re-exports the public classes and pulls in the side-effect modules that patch Leaflet’s global L namespace.
Tech Stack: Pure vanilla JavaScript (ES5-style, no TypeScript) targeting Leaflet 1.x as a peer dependency (^1.3.1). The build pipeline uses Rollup (rollup-plugin-git-version, rollup-plugin-json) to bundle src/ into the UMD dist/leaflet.markercluster-src.js, then UglifyJS produces the minified dist/leaflet.markercluster.js with source maps; Jakefile.js orchestrates the full build+lint+test flow via the jake task runner (a legacy but still-functional Grunt/Gulp-era tool). No bundler-specific packaging (no ESM exports map) — consumers get the module via main/style fields in package.json pointing at the prebuilt dist/ files, or via CDN/npm install.
Code Quality: The spec/suites/ directory holds 27 Karma+Mocha+Sinon test files (AddLayer.MultipleSpec.js, spiderfySpec.js, zoomAnimationSpec.js, disableClusteringAtZoom Spec.js, PaneSpec.js, and more) covering add/remove flows, spiderfy behavior, chunked loading, negative-zoom edge cases, and non-point-geometry support (circles, circle markers) — a genuinely broad suite for a UI plugin of this size, run across Chrome/Firefox/PhantomJS/Safari launchers in CI (.travis.yml). Code style is pre-ES6 (var, prototype extend() calls, no classes) and uses JSHint rather than ESLint for linting; naming is consistent Leaflet-house-style (_camelCase for private members). No TypeScript types ship in the package itself (community @types/leaflet.markercluster exists separately on DefinitelyTyped).
API Design: The API deliberately mirrors Leaflet’s own conventions so it feels native rather than bolted-on: L.markerClusterGroup(options) returns an object usable anywhere an L.LayerGroup is expected, and the same addLayer/on('click', ...) idioms apply, with cluster-specific events namespaced as clusterclick/clustermouseover to avoid clashing with individual-marker events on the same group. Getting started requires only two lines (create the group, add it to the map) beyond normal Leaflet setup, and nearly every visual behavior (spiderfy, coverage-on-hover, zoom-to-bounds, custom icons) is a single boolean or callback option rather than a subclass or plugin-within-a-plugin. The tradeoff is an implicit dependency on Leaflet’s global L object and prototype-extension pattern, which reads as dated next to modern ESM-first libraries but is entirely consistent with the ecosystem it plugs into.
Used by 3 apps in this directory
memos
Note Taking
Open-source, self-hosted note-taking built for quick capture — Markdown-native, lightweight, and fully yours.
NocoDB
No Code Platforms · Databases · Low Code Platforms
Turn any SQL database into a collaborative no-code spreadsheet with automatic REST APIs and real-time views.
Redash
Analytics · Data Engineering
Redash lets anyone connect to 35+ SQL and NoSQL data sources, write a query in the browser, and turn the result into a shared dashboard — no separate BI suite required.