Atlaslog
Atlaslog / Examples / Cesium / Czml
Cesium · Runnable Example

Czml

Runnable Cesium example: Czml. 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-czml.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/cesium-czml.html
<!--
  示例:用 CZML 在地球上播放一条飞行动画
  要点:CzmlDataSource 加载内联 CZML、clock 控制时间轴、path 自动画出轨迹
  依赖: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>用 CZML 播放一条飞行动画</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%}
  .bar{position:absolute;left:12px;top:12px;z-index:5;display:flex;gap:8px;align-items:center;
       background:rgba(23,28,35,.9);border:1px solid #2a313b;border-radius:6px;padding:7px 10px}
  .bar button{font:inherit;font-size:12px;padding:4px 10px;border-radius:4px;cursor:pointer;
       border:1px solid #343b46;background:#1d232b;color:#cfd6e2}
  .bar button:hover{background:#262d37}
  .bar span{font-size:12px;color:#8b93a1}
  .cesium-viewer-bottom .cesium-widget-credits,.cesium-viewer-animationContainer,
  .cesium-viewer-timelineContainer{display:none!important}
</style>
</head>
<body>
<div id="c"></div>
<div class="bar">
  <button id="toggle">暂停</button>
  <span id="state">飞行中</span>
</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 route = [
    [116.391263, 39.907359, 120000, '北京'],
    [121.473701, 31.230416, 120000, '上海'],
    [113.264385, 23.129112, 120000, '广州'],
    [104.066541, 30.572269, 120000, '成都'],
    [87.616848, 43.825592, 120000, '乌鲁木齐']
  ];

  var START = '2026-01-01T00:00:00Z';
  var STEP = 45; /* 每段用时 45 秒 */

  /* 构造 cartographicDegrees:[时间偏移秒, 经度, 纬度, 高度, ...] */
  var samples = [];
  route.forEach(function (p, i) {
    samples.push(i * STEP, p[0], p[1], p[2]);
  });

  var czml = [
    { id: 'document', version: '1.0', clock: {
        interval: START + '/2026-01-01T00:03:45Z',
        currentTime: START,
        multiplier: 12,
        range: 'LOOP_STOP',
        step: 'SYSTEM_CLOCK_MULTIPLIER'
      }
    },
    {
      id: 'flight',
      name: '航线',
      availability: START + '/2026-01-01T00:03:45Z',
      position: { epoch: START, cartographicDegrees: samples },
      point: {
        pixelSize: 10,
        color: { rgba: [255, 170, 60, 255] },
        outlineColor: { rgba: [255, 255, 255, 255] },
        outlineWidth: 2
      },
      path: {
        resolution: 1,
        width: 2,
        material: { polyline: { color: { rgba: [255, 170, 60, 200] } } }
      },
      label: {
        text: '航线',
        font: '600 12px sans-serif',
        fillColor: { rgba: [255, 255, 255, 255] },
        pixelOffset: { cartesian2: [0, -22] }
      }
    }
  ];

  /* 同时把五个城市标出来,作为航点参考 */
  route.forEach(function (p) {
    viewer.entities.add({
      position: Cesium.Cartesian3.fromDegrees(p[0], p[1]),
      point: { pixelSize: 6, color: Cesium.Color.fromCssColorString('#4d9bff') },
      label: {
        text: p[3],
        font: '500 11px sans-serif',
        fillColor: Cesium.Color.fromCssColorString('#9fb6d4'),
        pixelOffset: new Cesium.Cartesian2(0, -16),
        disableDepthTestDistance: Number.POSITIVE_INFINITY
      }
    });
  });

  Cesium.CzmlDataSource.load(czml).then(function (ds) {
    viewer.dataSources.add(ds);

    /* clock 跟随 CZML 里声明的时间区间 */
    viewer.clock.startTime = Cesium.JulianDate.fromIso8601(START);
    viewer.clock.stopTime = Cesium.JulianDate.fromIso8601('2026-01-01T00:03:45Z');
    viewer.clock.currentTime = Cesium.JulianDate.fromIso8601(START);
    viewer.clock.multiplier = 12;
    viewer.clock.shouldAnimate = true;

    viewer.camera.flyTo({
      destination: Cesium.Cartesian3.fromDegrees(105, 32, 6500000),
      orientation: { heading: 0, pitch: Cesium.Math.toRadians(-88), roll: 0 },
      duration: 0
    });
  });

  var paused = false;
  document.getElementById('toggle').addEventListener('click', function () {
    paused = !paused;
    viewer.clock.shouldAnimate = !paused;
    this.textContent = paused ? '继续' : '暂停';
    document.getElementById('state').textContent = paused ? '已暂停' : '飞行中';
  });
})();
</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 · EXAMPLECzml 3D Tiles Cesium · EXAMPLECzml Animated Property Cesium · EXAMPLECzml Billboard Label