Atlaslog
Atlaslog / Examples / Cesium / Clustering
Cesium · Runnable Example

Clustering

Runnable Cesium example: Clustering. Open the live demo, read the complete source, and copy the file into your project. No build step or API key.

Open standalone → Chinese notes All Cesium examples
/demos/cesium-clustering.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/cesium-clustering.html
<!--
  示例:用 EntityCluster 把邻近实体聚合成一个标签
  说明:自定义数据源打开聚类,clusterEvent 里接管聚合体的样式与文字
  核心逻辑:dataSource.clustering.enabled + pixelRange + clusterEvent
  依赖:CesiumJS 1.121 · jsDelivr CDN
-->
<!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/cesium@1.121.0/Build/Cesium/Widgets/widgets.css">
<style>
  html,body{height:100%;margin:0}
  body{background:#0e1116;color:#e6e8ec;overflow:hidden;
       font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif}
  #c{height:100%}
  .hud{position:absolute;left:12px;top:12px;z-index:5;background:rgba(23,28,35,.9);
       border:1px solid #2a313b;border-radius:6px;padding:9px 12px;font-size:12px;
       color:#a2a9b6;max-width:250px}
  .hud b{color:#e6e8ec;font-weight:600}
  .hud .row{margin-top:7px}
  input[type=range]{width:100%}
  .cesium-viewer-bottom .cesium-widget-credits,.cesium-viewer-animationContainer,
  .cesium-viewer-timelineContainer{display:none!important}
</style>
</head>
<body>
<div id="c"></div>
<div class="hud">
  <div><b>实体聚合</b></div>
  <div class="row">共 480 个点,最少 3 个才聚合</div>
  <div class="row">聚合半径 <span id="rVal">48</span> px</div>
  <div class="row"><input id="r" type="range" min="12" max="120" step="4" value="48"></div>
</div>
<script src="https://cdn.jsdelivr.net/npm/cesium@1.121.0/Build/Cesium/Cesium.js"></script>
<script>
(function () {
  'use strict';

  var viewer = new Cesium.Viewer('c', {
    baseLayer: false,
    baseLayerPicker: false, geocoder: false, homeButton: false,
    sceneModePicker: false, navigationHelpButton: false,
    animation: false, timeline: false, fullscreenButton: false,
    infoBox: false, selectionIndicator: false
  });

  var scene = viewer.scene;
  scene.globe.baseColor = Cesium.Color.fromCssColorString('#1c2733');
  scene.backgroundColor = Cesium.Color.fromCssColorString('#0e1116');
  scene.skyBox.show = false;
  scene.sun.show = false;
  scene.moon.show = false;
  if (scene.skyAtmosphere) scene.skyAtmosphere.show = false;

  var dataSource = new Cesium.CustomDataSource('samples');
  viewer.dataSources.add(dataSource);

  /* 在华北一带撒 480 个点,形成若干密集区域 */
  var centers = [
    [116.39, 39.90], [121.47, 31.23], [113.26, 23.12],
    [104.07, 30.57], [108.95, 34.34], [114.30, 30.59]
  ];

  for (var c = 0; c < centers.length; c++) {
    for (var i = 0; i < 80; i++) {
      var ang = (i * 2.399) % (Math.PI * 2);
      var r = 0.02 + ((i * 37) % 100) / 1000;
      dataSource.entities.add({
        position: Cesium.Cartesian3.fromDegrees(
          centers[c][0] + r * Math.cos(ang),
          centers[c][1] + r * Math.sin(ang) * 0.8,
          0
        ),
        point: {
          pixelSize: 6,
          color: Cesium.Color.fromCssColorString('#4d9bff').withAlpha(0.85),
          outlineColor: Cesium.Color.fromCssColorString('#0e1116'),
          outlineWidth: 1
        }
      });
    }
  }

  var clustering = dataSource.clustering;
  clustering.enabled = true;
  clustering.pixelRange = 48;
  clustering.minimumClusterSize = 3;

  clustering.clusterEvent.addEventListener(function (clusteredEntities, cluster) {
    cluster.billboard.show = false;
    cluster.point.show = false;

    var n = clusteredEntities.length;
    cluster.label.show = true;
    cluster.label.text = String(n);
    cluster.label.font = '600 14px "Microsoft YaHei", sans-serif';
    cluster.label.fillColor = Cesium.Color.WHITE;
    cluster.label.outlineColor = Cesium.Color.fromCssColorString('#0e1116');
    cluster.label.outlineWidth = 4;
    cluster.label.style = Cesium.LabelStyle.FILL_AND_OUTLINE;
    cluster.label.horizontalOrigin = Cesium.HorizontalOrigin.CENTER;
    cluster.label.verticalOrigin = Cesium.VerticalOrigin.CENTER;
    cluster.label.showBackground = true;
    cluster.label.backgroundColor = n > 40
      ? Cesium.Color.fromCssColorString('#e45c7a')
      : Cesium.Color.fromCssColorString('#2d6fb8');
    cluster.label.backgroundPadding = new Cesium.Cartesian2(10, 6);
    cluster.label.disableDepthTestDistance = Number.POSITIVE_INFINITY;
  });

  document.getElementById('r').addEventListener('input', function () {
    clustering.pixelRange = parseInt(this.value, 10);
    document.getElementById('rVal').textContent = clustering.pixelRange;
  });

  viewer.camera.flyTo({
    destination: Cesium.Cartesian3.fromDegrees(112, 33, 6800000),
    orientation: {
      heading: 0,
      pitch: Cesium.Math.toRadians(-72),
      roll: 0
    },
    duration: 0
  });
})();
</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 Cesium examples

Cesium · EXAMPLECorridor Band Cesium · EXAMPLECustom Datasource Cesium · EXAMPLECustom Geocoder