Ground Label
Runnable Cesium example: Ground Label. 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
<!--
示例:贴地标牌与深度测试
要点:label/point 设 heightReference 贴地,disableDepthTestDistance 归零后由深度测试决定遮挡
依赖: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:252px}
.hud b{color:#e6e8ec;font-weight:600}
.hud .row{margin-top:7px}
.hud button{font:inherit;font-size:12px;padding:3px 9px;border-radius:4px;cursor:pointer;
border:1px solid #343b46;background:#1d232b;color:#cfd6e2}
.hud button.on{background:#1d3a5c;border-color:#3d6f9e;color:#cfe4ff}
.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"><button id="depth" class="on">深度测试:开</button></div>
<div class="row" id="info">标牌不会穿透地球背面</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;
scene.globe.depthTestAgainstTerrain = true;
var SITES = [
{ lng: 117.100000, lat: 36.250000, name: '泰山' },
{ lng: 110.080000, lat: 34.480000, name: '华山' },
{ lng: 103.330000, lat: 29.520000, name: '峨眉山' },
{ lng: 86.925000, lat: 27.988000, name: '珠穆朗玛峰' },
{ lng: 128.060000, lat: 42.000000, name: '长白山' },
{ lng: 86.000000, lat: 43.000000, name: '天山' }
];
SITES.forEach(function (s) {
viewer.entities.add({
position: Cesium.Cartesian3.fromDegrees(s.lng, s.lat, 0),
point: {
pixelSize: 7,
color: Cesium.Color.fromCssColorString('#ffb13d'),
outlineColor: Cesium.Color.WHITE,
outlineWidth: 1.5,
heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
disableDepthTestDistance: 0
},
label: {
text: s.name,
font: '500 12px "Microsoft YaHei", sans-serif',
style: Cesium.LabelStyle.FILL_AND_OUTLINE,
fillColor: Cesium.Color.WHITE,
outlineColor: Cesium.Color.fromCssColorString('#0e1116'),
outlineWidth: 3,
pixelOffset: new Cesium.Cartesian2(0, -18),
verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
disableDepthTestDistance: 0
}
});
});
var on = true;
var btn = document.getElementById('depth');
var info = document.getElementById('info');
btn.addEventListener('click', function () {
on = !on;
if (on) {
scene.globe.depthTestAgainstTerrain = true;
btn.textContent = '深度测试:开';
btn.className = 'on';
info.textContent = '标牌不会穿透地球背面';
} else {
scene.globe.depthTestAgainstTerrain = false;
btn.textContent = '深度测试:关';
btn.className = '';
info.textContent = '标牌浮在最前,背面也可见';
}
var list = viewer.entities.values;
for (var i = 0; i < list.length; i++) {
var e = list[i];
if (e.label) e.label.disableDepthTestDistance = on ? 0 : Number.POSITIVE_INFINITY;
if (e.point) e.point.disableDepthTestDistance = on ? 0 : Number.POSITIVE_INFINITY;
}
});
viewer.camera.flyTo({
destination: Cesium.Cartesian3.fromDegrees(104, 32, 12000000),
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.