Custom Datasource
Runnable Cesium example: Custom Datasource. Open the live demo, read the complete source, and copy the file into your project. No build step or API key.
single HTML file · CDN dependencies onlyinterface may contain Chinese labels
Source
<!--
示例:自己实现一个数据源,把外部数组接进 Cesium
说明:重写 load 与 update,load 里建实体,update 每帧推进目标点位置
核心逻辑:new CustomDataSource → 覆盖 load/update → viewer.dataSources.add
依赖: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:260px}
.hud b{color:#e6e8ec;font-weight:600}
.hud .row{margin-top:7px}
.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">每条轨迹是一个独立实体组</div>
<div class="row"><label><input id="show" type="checkbox" checked> 显示数据源</label></div>
<div class="row" id="info">3 条轨迹 · 每帧由 update 推进</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.moon.show = false;
if (scene.skyAtmosphere) scene.skyAtmosphere.show = false;
/* ---------- 数据:三条轨迹的中心、半径、速度 ---------- */
var TRACKS = [
{ name: '轨迹 A', lon: 116.20, lat: 39.95, r: 0.05, speed: 0.35, color: '#4d9bff' },
{ name: '轨迹 B', lon: 116.55, lat: 39.90, r: 0.06, speed: 0.22, color: '#39c0a0' },
{ name: '轨迹 C', lon: 116.36, lat: 39.75, r: 0.04, speed: 0.5, color: '#f2a03d' }
];
var dataSource = new Cesium.CustomDataSource('tracks');
var movers = [];
/* ---------- load:把外部数据变成实体 ---------- */
dataSource.load = function (tracks) {
var self = this;
tracks.forEach(function (t) {
var pts = [];
for (var i = 0; i <= 64; i++) {
var a = (i / 64) * Math.PI * 2;
pts.push(t.lon + t.r * Math.cos(a), t.lat + t.r * 0.8 * Math.sin(a), 600);
}
self.entities.add({
name: t.name + ' 路径',
polyline: {
positions: Cesium.Cartesian3.fromDegreesArrayHeights(pts),
width: 2,
material: Cesium.Color.fromCssColorString(t.color).withAlpha(0.45)
}
});
var mover = self.entities.add({
name: t.name,
position: Cesium.Cartesian3.fromDegrees(t.lon + t.r, t.lat, 600),
point: {
pixelSize: 11,
color: Cesium.Color.fromCssColorString(t.color),
outlineColor: Cesium.Color.WHITE,
outlineWidth: 2,
disableDepthTestDistance: Number.POSITIVE_INFINITY
},
label: {
text: t.name,
font: '500 12px "Microsoft YaHei", sans-serif',
fillColor: Cesium.Color.fromCssColorString(t.color),
pixelOffset: new Cesium.Cartesian2(0, -20),
disableDepthTestDistance: Number.POSITIVE_INFINITY
}
});
movers.push({ track: t, entity: mover });
});
return Promise.resolve(self);
};
/* ---------- update:每帧被数据源显示循环调用 ---------- */
var elapsed = 0;
dataSource.update = function (time) {
elapsed += 1 / 60;
movers.forEach(function (m) {
var a = elapsed * m.track.speed;
m.entity.position = Cesium.Cartesian3.fromDegrees(
m.track.lon + m.track.r * Math.cos(a),
m.track.lat + m.track.r * 0.8 * Math.sin(a),
600
);
});
return true;
};
dataSource.load(TRACKS).then(function (ds) {
viewer.dataSources.add(ds);
});
document.getElementById('show').addEventListener('change', function () {
dataSource.show = this.checked;
});
viewer.camera.flyTo({
destination: Cesium.Cartesian3.fromDegrees(116.375, 39.845, 42000),
orientation: {
heading: 0,
pitch: Cesium.Math.toRadians(-38),
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.