Entity Drag
Runnable Cesium example: Entity Drag. 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
<!--
示例:按住方块把它拖到新位置
要点:LEFT_DOWN 拾取实体并锁住相机旋转,MOUSE_MOVE 改 position,LEFT_UP 解锁
依赖: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="state">按住方块拖动</div>
<div class="row mono" id="pos">经度 — · 纬度 —</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.380, lat: 39.904, color: '#4d9bff' },
{ name: '乙', lng: 116.391, lat: 39.900, color: '#5fd0a8' },
{ name: '丙', lng: 116.401, lat: 39.907, color: '#ffb13d' }
];
var boxes = [];
BLOCKS.forEach(function (b) {
var entity = viewer.entities.add({
position: Cesium.Cartesian3.fromDegrees(b.lng, b.lat, 130),
box: {
dimensions: new Cesium.Cartesian3(220, 220, 260),
material: Cesium.Color.fromCssColorString(b.color).withAlpha(0.75),
outline: true,
outlineColor: Cesium.Color.fromCssColorString('#eaf2ff')
},
label: {
text: b.name,
font: '600 13px "Microsoft YaHei", sans-serif',
fillColor: Cesium.Color.WHITE,
pixelOffset: new Cesium.Cartesian2(0, -8),
disableDepthTestDistance: Number.POSITIVE_INFINITY
}
});
boxes.push(entity);
});
var handler = new Cesium.ScreenSpaceEventHandler(scene.canvas);
var dragging = null;
var stateEl = document.getElementById('state');
var posEl = document.getElementById('pos');
function groundAt(windowPosition) {
var ray = viewer.camera.getPickRay(windowPosition);
return ray ? scene.globe.pick(ray, scene) : undefined;
}
handler.setInputAction(function (down) {
var picked = scene.pick(down.position);
if (!Cesium.defined(picked) || !Cesium.defined(picked.id) ||
!Cesium.defined(picked.id.box)) {
return;
}
dragging = picked.id;
scene.screenSpaceCameraController.enableRotate = false;
dragging.box.material = Cesium.Color.fromCssColorString('#ffd18a').withAlpha(0.35);
stateEl.textContent = '正在拖动 ' + dragging.label.text.getValue(viewer.clock.currentTime);
}, Cesium.ScreenSpaceEventType.LEFT_DOWN);
handler.setInputAction(function (move) {
if (!dragging) return;
var p = groundAt(move.endPosition);
if (!Cesium.defined(p)) return;
var carto = Cesium.Cartographic.fromCartesian(p);
/* 保持方块原本的离地高度,只改经纬度 */
dragging.position = Cesium.Cartesian3.fromRadians(
carto.longitude, carto.latitude, 130
);
posEl.textContent = '经度 ' + Cesium.Math.toDegrees(carto.longitude).toFixed(4) +
' · 纬度 ' + Cesium.Math.toDegrees(carto.latitude).toFixed(4);
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
handler.setInputAction(function () {
if (!dragging) return;
var idx = boxes.indexOf(dragging);
dragging.box.material = Cesium.Color.fromCssColorString(
BLOCKS[idx].color
).withAlpha(0.75);
dragging = null;
scene.screenSpaceCameraController.enableRotate = true;
stateEl.textContent = '已放下,可继续拖动其他方块';
}, Cesium.ScreenSpaceEventType.LEFT_UP);
viewer.camera.flyTo({
destination: Cesium.Cartesian3.fromDegrees(116.372, 39.892, 2600),
orientation: {
heading: Cesium.Math.toRadians(22),
pitch: Cesium.Math.toRadians(-36),
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.