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

Czml Custom Properties

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

Source

demos/cesium-czml-custom-properties.html
<!--
  示例:把自定义字段写进 CZML,点击实体读出
  说明:人口、等级、面积这些字段直接放在对象顶层,Cesium 收进 entity.properties
  核心逻辑:ScreenSpaceEventHandler + scene.pick → pick.id.properties.xxx.getValue()
  依赖: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%}
  .panel{position:absolute;right:12px;top:12px;z-index:5;background:rgba(23,28,35,.94);
       border:1px solid #2a313b;border-radius:6px;padding:10px 14px;font-size:12px;
       color:#a2a9b6;min-width:200px;line-height:1.9}
  .panel b{color:#e6e8ec;font-weight:600}
  .panel .k{color:#7d8998;margin-right:6px}
  .panel .v{color:#e6e8ec}
  .cesium-viewer-bottom .cesium-widget-credits,.cesium-viewer-animationContainer,
  .cesium-viewer-timelineContainer{display:none!important}
</style>
</head>
<body>
<div id="c"></div>
<div class="panel">
  <div><b>点一个城市</b></div>
  <div id="props">还没有选中实体</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 cities = [
    [116.391263, 39.907359, '北京', 2189, '一线', 16410],
    [121.473701, 31.230416, '上海', 2487, '一线', 6340],
    [113.264385, 23.129112, '广州', 1873, '一线', 7434],
    [104.066541, 30.572269, '成都', 2119, '新一线', 14335],
    [87.616848, 43.825592, '乌鲁木齐', 405, '二线', 14216],
    [114.305, 30.593, '武汉', 1365, '新一线', 8569]
  ];

  var data = [{ id: 'document', name: '自定义属性', version: '1.0' }];

  cities.forEach(function (c, i) {
    data.push({
      id: 'city-' + i,
      name: c[2],
      position: { cartographicDegrees: [c[0], c[1], 0] },
      point: {
        pixelSize: 11,
        color: { rgba: [77, 155, 255, 255] },
        outlineColor: { rgba: [255, 255, 255, 255] },
        outlineWidth: 2,
        disableDepthTestDistance: Number.POSITIVE_INFINITY
      },
      label: {
        text: c[2],
        font: '500 13px "Microsoft YaHei", sans-serif',
        fillColor: { rgba: [230, 240, 255, 255] },
        outlineColor: { rgba: [14, 17, 22, 255] },
        outlineWidth: 3,
        style: 'FILL_AND_OUTLINE',
        pixelOffset: { cartesian2: [0, -22] },
        disableDepthTestDistance: Number.POSITIVE_INFINITY
      },
      人口: c[3],
      等级: c[4],
      面积: c[5]
    });
  });

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

  var handler = new Cesium.ScreenSpaceEventHandler(scene.canvas);
  var infoEl = document.getElementById('props');

  handler.setInputAction(function (click) {
    var picked = scene.pick(click.position);
    var el = picked && picked.id;

    if (!el || !el.properties) {
      infoEl.innerHTML = '还没有选中实体';
      return;
    }

    var time = viewer.clock.currentTime;
    var bag = el.properties;
    function read(key) {
      var p = bag[key];
      return p ? p.getValue(time) : '—';
    }

    infoEl.innerHTML =
      '<div><span class="k">名称</span><span class="v">' + el.name + '</span></div>' +
      '<div><span class="k">人口</span><span class="v">' + read('人口') + ' 万</span></div>' +
      '<div><span class="k">等级</span><span class="v">' + read('等级') + '</span></div>' +
      '<div><span class="k">面积</span><span class="v">' + read('面积') + ' km²</span></div>';
  }, Cesium.ScreenSpaceEventType.LEFT_CLICK);

  viewer.camera.flyTo({
    destination: Cesium.Cartesian3.fromDegrees(110, 34, 5200000),
    orientation: {
      heading: 0,
      pitch: Cesium.Math.toRadians(-90),
      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 · EXAMPLECzml Model Cesium · EXAMPLECzml Model Articulations Cesium · EXAMPLECzml Model Data Url