Atlaslog
Atlaslog / Examples / MapLibre GL JS / Cluster
MapLibre GL JS · Runnable Example

Cluster

Cluster a GeoJSON source with expanding circles and counts.

Open standalone → Chinese notes All MapLibre GL JS examples
/demos/maplibre-cluster.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/maplibre-cluster.html
<!--
  示例:点聚合
  核心逻辑:geojson 源开 cluster,三层分别画簇、簇计数与单点,点击簇按展开级别放大
  依赖:MapLibre GL JS 4 · jsDelivr CDN · demotiles 样式
-->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta name="robots" content="noindex,follow">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>点聚合</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/maplibre-gl@4/dist/maplibre-gl.css">
<style>
  html,body{height:100%;margin:0}
  body{display:flex;flex-direction:column;overflow:hidden;
       font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif;color:#16181d}
  .bar{flex:0 0 auto;display:flex;gap:12px;align-items:center;padding:8px 12px;
       background:#f6f7f9;border-bottom:1px solid #e3e6ea;font-size:12px;color:#58606e;flex-wrap:wrap}
  .bar b{color:#16181d;font-weight:600}
  #map{flex:1 1 auto;min-height:0}
</style>
</head>
<body>
<div class="bar">
  <span>点总数 <b>320</b></span>
  <span>缩放 <b id="z">—</b></span>
  <span>点击圆簇可展开,点击单点查看坐标</span>
</div>
<div id="map"></div>
<script src="https://cdn.jsdelivr.net/npm/maplibre-gl@4/dist/maplibre-gl.js"></script>
<script>
(function () {
  'use strict';

  if (typeof maplibregl === 'undefined') {
    document.body.innerHTML =
      '<div style="padding:24px;color:#b42318">MapLibre 加载失败,请检查能否访问 cdn.jsdelivr.net</div>';
    return;
  }

  var features = [];
  for (var i = 0; i < 320; i++) {
    features.push({
      type: 'Feature',
      properties: { id: i },
      geometry: {
        type: 'Point',
        coordinates: [
          118.0 + Math.random() * 4.2,
          29.0 + Math.random() * 4.2
        ]
      }
    });
  }

  var map = new maplibregl.Map({
    container: 'map',
    style: 'https://demotiles.maplibre.org/style.json',
    center: [120.1, 31.1],
    zoom: 5,
    attributionControl: { compact: true, customAttribution: 'MapLibre demo tiles' }
  });

  map.addControl(new maplibregl.NavigationControl(), 'top-right');

  map.on('load', function () {
    map.addSource('sites', {
      type: 'geojson',
      data: { type: 'FeatureCollection', features: features },
      cluster: true,
      clusterRadius: 48,
      clusterMaxZoom: 8
    });

    map.addLayer({
      id: 'clusters',
      type: 'circle',
      source: 'sites',
      filter: ['has', 'point_count'],
      paint: {
        'circle-color': [
          'step', ['get', 'point_count'],
          '#9ec5e8', 10,
          '#4a90c9', 30,
          '#1f5fd0'
        ],
        'circle-radius': [
          'step', ['get', 'point_count'],
          16, 10, 21, 30, 27
        ],
        'circle-opacity': 0.85
      }
    });

    map.addLayer({
      id: 'cluster-count',
      type: 'symbol',
      source: 'sites',
      filter: ['has', 'point_count'],
      layout: {
        'text-field': ['get', 'point_count_abbreviated'],
        'text-font': ['Noto Sans Regular'],
        'text-size': 12
      },
      paint: { 'text-color': '#ffffff' }
    });

    map.addLayer({
      id: 'sites',
      type: 'circle',
      source: 'sites',
      filter: ['!', ['has', 'point_count']],
      paint: {
        'circle-color': '#e8590c',
        'circle-radius': 4,
        'circle-stroke-color': '#ffffff',
        'circle-stroke-width': 1
      }
    });

    map.on('click', 'clusters', function (e) {
      var f = e.features[0];
      var src = map.getSource('sites');
      src.getClusterExpansionZoom(f.properties.cluster_id).then(function (zoom) {
        map.easeTo({ center: f.geometry.coordinates, zoom: zoom, duration: 500 });
      });
    });

    map.on('mouseenter', 'clusters', function () {
      map.getCanvas().style.cursor = 'pointer';
    });
    map.on('mouseleave', 'clusters', function () {
      map.getCanvas().style.cursor = '';
    });
  });

  map.on('moveend', function () {
    document.getElementById('z').textContent = map.getZoom().toFixed(2);
  });

  map.on('error', function (e) {
    if (e.error) console.warn('地图错误:' + e.error.message);
  });
})();
</script>
</body>
</html>
Language: the code and this page are in English; the full tutorial and API notes for this example are in Chinese. The demo itself is the authoritative version — it runs exactly as shown.

More MapLibre GL JS examples

MapLibre GL JS · EXAMPLECustom Control MapLibre GL JS · EXAMPLECustom Icon MapLibre GL JS · EXAMPLEData Driven Line