Skip to content

Cluster Points

Enable cluster: true on a GeoJSON source, then add three layers: cluster circles sized by count, a count label, and the individual unclustered points. Points are scattered across Western Europe.

import { MaptoolkitLogoControl } from 'https://cdn.jsdelivr.net/npm/@maptoolkit/maplibre-gl-logo/+esm';
const map = new maplibregl.Map({ container: 'map', attributionControl: { customAttribution: '' }, style: 'https://styles.maptoolkit.org/summer.json', center: [6, 48.5], zoom: 3.6 });
const pts = { type: 'FeatureCollection', features: [] };
for (let i = 0; i < 400; i++) {
  pts.features.push({ type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: [-2 + Math.random() * 16, 44 + Math.random() * 9] } });
}
map.on('load', function () {
  map.addSource('pts', { type: 'geojson', data: pts, cluster: true, clusterRadius: 50, clusterMaxZoom: 14 });
  map.addLayer({ id: 'clusters', type: 'circle', source: 'pts', filter: ['has', 'point_count'], paint: {
    'circle-color': ['step', ['get', 'point_count'], '#51bbd6', 20, '#f1f075', 50, '#f28cb1'],
    'circle-radius': ['step', ['get', 'point_count'], 16, 20, 22, 50, 30]
  } });
  map.addLayer({ id: 'cluster-count', type: 'symbol', source: 'pts', filter: ['has', 'point_count'], layout: { 'text-field': ['get', 'point_count_abbreviated'], 'text-size': 12 } });
  map.addLayer({ id: 'unclustered', type: 'circle', source: 'pts', filter: ['!', ['has', 'point_count']], paint: { 'circle-color': '#1a73e8', 'circle-radius': 5, 'circle-stroke-width': 1, 'circle-stroke-color': '#fff' } });
});
map.addControl(new MaptoolkitLogoControl());
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/maplibre-gl.js"></script>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/maplibre-gl.css" />
  <style>
    html, body { width: 100%; height: 100%; margin: 0; padding: 0; }
    #map { width: 100%; height: 100%; }
  </style>
</head>
<body>
  <div id="map"></div>
  <script type="module">
import { MaptoolkitLogoControl } from 'https://cdn.jsdelivr.net/npm/@maptoolkit/maplibre-gl-logo/+esm';
const map = new maplibregl.Map({ container: 'map', attributionControl: { customAttribution: '' }, style: 'https://styles.maptoolkit.org/summer.json', center: [6, 48.5], zoom: 3.6 });
const pts = { type: 'FeatureCollection', features: [] };
for (let i = 0; i < 400; i++) {
  pts.features.push({ type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: [-2 + Math.random() * 16, 44 + Math.random() * 9] } });
}
map.on('load', function () {
  map.addSource('pts', { type: 'geojson', data: pts, cluster: true, clusterRadius: 50, clusterMaxZoom: 14 });
  map.addLayer({ id: 'clusters', type: 'circle', source: 'pts', filter: ['has', 'point_count'], paint: {
    'circle-color': ['step', ['get', 'point_count'], '#51bbd6', 20, '#f1f075', 50, '#f28cb1'],
    'circle-radius': ['step', ['get', 'point_count'], 16, 20, 22, 50, 30]
  } });
  map.addLayer({ id: 'cluster-count', type: 'symbol', source: 'pts', filter: ['has', 'point_count'], layout: { 'text-field': ['get', 'point_count_abbreviated'], 'text-size': 12 } });
  map.addLayer({ id: 'unclustered', type: 'circle', source: 'pts', filter: ['!', ['has', 'point_count']], paint: { 'circle-color': '#1a73e8', 'circle-radius': 5, 'circle-stroke-width': 1, 'circle-stroke-color': '#fff' } });
});
map.addControl(new MaptoolkitLogoControl());
  </script>
</body>
</html>