Pick Highlight
Runnable Cesium example: Pick Highlight. 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
<!--
示例:点击实体后高亮并读出坐标
要点:ScreenSpaceEventHandler 监听 LEFT_CLICK,scene.pick 拿到 Entity 再改样式
依赖: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,.92);
border:1px solid #2a313b;border-radius:6px;padding:9px 12px;font-size:12px;
color:#a2a9b6;width:252px}
.hud b{color:#e6e8ec;font-weight:600}
.hud .row{margin-top:6px}
.mono{font-family:ui-monospace,Consolas,monospace;font-size:11px;color:#8fb7e0}
.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" id="name">点一个方块试试</div>
<div class="row mono" id="pos">经度 — · 纬度 —</div>
<div class="row">选中后棱线变亮、点变大</div>
<div class="row">点空白处取消选中</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 BLOCKS = [
{ name: '一号', lng: 116.378, lat: 39.902, h: 300, color: '#4d9bff' },
{ name: '二号', lng: 116.390, lat: 39.900, h: 200, color: '#5fd0a8' },
{ name: '三号', lng: 116.400, lat: 39.906, h: 380, color: '#ffb13d' },
{ name: '四号', lng: 116.388, lat: 39.913, h: 240, color: '#e35d6a' }
];
var entities = [];
BLOCKS.forEach(function (b) {
var hover = viewer.entities.add({
position: Cesium.Cartesian3.fromDegrees(b.lng, b.lat, b.h / 2),
box: {
dimensions: new Cesium.Cartesian3(180, 180, b.h),
material: Cesium.Color.fromCssColorString(b.color).withAlpha(0.55),
outline: false
},
label: {
text: b.name,
font: '500 12px "Microsoft YaHei", sans-serif',
fillColor: Cesium.Color.WHITE,
pixelOffset: new Cesium.Cartesian2(0, -18),
disableDepthTestDistance: Number.POSITIVE_INFINITY
}
});
hover.baseColor = b.color;
hover.baseHeight = b.h;
entities.push(hover);
});
var selected = null;
var outline = Cesium.Color.fromCssColorString('#ffd18a');
function clear(entity) {
entity.box.outline = false;
entity.box.material = Cesium.Color.fromCssColorString(entity.baseColor).withAlpha(0.55);
entity.label.fillColor = Cesium.Color.WHITE;
}
function highlight(entity) {
entity.box.outline = true;
entity.box.outlineColor = outline;
entity.box.material = Cesium.Color.fromCssColorString('#ffd18a').withAlpha(0.28);
entity.label.fillColor = outline;
}
var handler = new Cesium.ScreenSpaceEventHandler(scene.canvas);
var nameEl = document.getElementById('name');
var posEl = document.getElementById('pos');
handler.setInputAction(function (movement) {
var picked = scene.pick(movement.position);
if (selected) {
clear(selected);
selected = null;
}
if (!Cesium.defined(picked) || !Cesium.defined(picked.id) ||
!Cesium.defined(picked.id.box)) {
nameEl.textContent = '未选中实体';
posEl.textContent = '经度 — · 纬度 —';
return;
}
selected = picked.id;
highlight(selected);
var carto = Cesium.Cartographic.fromCartesian(selected.position.getValue(viewer.clock.currentTime));
nameEl.textContent = '已选中 ' + selected.label.text.getValue(viewer.clock.currentTime);
posEl.textContent = '经度 ' + Cesium.Math.toDegrees(carto.longitude).toFixed(4) +
' · 纬度 ' + Cesium.Math.toDegrees(carto.latitude).toFixed(4);
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
/* 显示已进入拾取模式的提示 */
scene.canvas.style.cursor = 'pointer';
viewer.camera.flyTo({
destination: Cesium.Cartesian3.fromDegrees(116.374, 39.892, 1500),
orientation: {
heading: Cesium.Math.toRadians(18),
pitch: Cesium.Math.toRadians(-26),
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.