Gpx
Runnable Cesium example: Gpx. 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
<!--
示例:加载 GPX 轨迹
说明:一份内嵌 GPX 包含一段轨迹与一个航点,加载后轨迹贴地显示
核心逻辑:GpxDataSource.load(data URL) → 轨迹转折线 → 航点转点
依赖: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>加载 GPX 轨迹</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%}
.tip{position:absolute;left:12px;top:12px;z-index:5;
background:rgba(23,28,35,.9);border:1px solid #2a313b;border-radius:6px;
padding:8px 12px;font-size:12px;color:#a2a9b6;max-width:320px;line-height:1.6}
.cesium-viewer-bottom .cesium-widget-credits,.cesium-viewer-animationContainer,
.cesium-viewer-timelineContainer{display:none!important}
</style>
</head>
<body>
<div id="c"></div>
<div class="tip">轨迹是带时间的折线,航点单独成为标注,两者都来自同一份 GPX。</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 pts = [];
for (var i = 0; i < 24; i++) {
var lon = 116.3850 + i * 0.00040 + Math.sin(i * 0.6) * 0.00018;
var lat = 39.9015 + i * 0.00013 + Math.cos(i * 0.5) * 0.00016;
var ele = 40 + Math.sin(i * 0.4) * 8;
pts.push('<trkpt lat="' + lat.toFixed(6) + '" lon="' + lon.toFixed(6) + '">' +
'<ele>' + ele.toFixed(1) + '</ele></trkpt>');
}
var gpx = [
'<?xml version="1.0" encoding="UTF-8"?>',
'<gpx version="1.1" creator="demo" xmlns="http://www.topografix.com/GPX/1/1">',
'<wpt lat="39.9042" lon="116.3930"><name>补给点</name></wpt>',
'<trk><name>示例轨迹</name><trkseg>',
pts.join(''),
'</trkseg></trk>',
'</gpx>'
].join('');
var dataUrl = 'data:application/gpx+xml;charset=utf-8,' + encodeURIComponent(gpx);
var ds = viewer.dataSources.add(Cesium.GpxDataSource.load(dataUrl));
ds.then(function (dataSource) {
viewer.zoomTo(dataSource, new Cesium.HeadingPitchRange(
Cesium.Math.toRadians(10), Cesium.Math.toRadians(-32), 420
));
});
/* 相机在数据加载完成后由 zoomTo 设置 */
})();
</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.