Camera Set View
Runnable Cesium example: Camera Set View. 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
<!--
示例:用 setView 与 lookAt 固定观察位置
要点:setView 直接给相机位姿,lookAt 以目标点加距离与角度反推相机
依赖: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:6px}
.hud button{font:inherit;font-size:12px;padding:3px 9px;margin:0 4px 4px 0;border-radius:4px;
cursor:pointer;border:1px solid #343b46;background:#1d232b;color:#cfd6e2}
.hud button.on{background:#1d3a5c;border-color:#3d6f9e;color:#cfe4ff}
.mono{font-family:ui-monospace,Consolas,monospace;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>setView 定点</b></div>
<div class="row">
<button id="p0" class="on">全球</button>
<button id="p1">城市</button>
<button id="p2">贴近</button>
</div>
<div class="row"><button id="p3">lookAt 目标点</button></div>
<div class="row mono" id="cam">高 — · 航向 —</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 TARGET = Cesium.Cartesian3.fromDegrees(116.389, 39.906, 0);
/* 一个方尖碑式的地标,作为观察对象 */
viewer.entities.add({
position: Cesium.Cartesian3.fromDegrees(116.389, 39.906, 150),
cylinder: {
length: 300, topRadius: 0, bottomRadius: 46,
material: Cesium.Color.fromCssColorString('#4d9bff').withAlpha(0.85),
outline: true,
outlineColor: Cesium.Color.fromCssColorString('#cfe4ff')
},
label: {
text: '目标点',
font: '500 12px "Microsoft YaHei", sans-serif',
fillColor: Cesium.Color.WHITE,
pixelOffset: new Cesium.Cartesian2(0, -12),
disableDepthTestDistance: Number.POSITIVE_INFINITY
}
});
/* 三个位姿:全球、城市、贴近地平线 */
var POSES = [
{
destination: Cesium.Cartesian3.fromDegrees(105, 35, 9000000),
orientation: { heading: 0, pitch: Cesium.Math.toRadians(-90), roll: 0 }
},
{
destination: Cesium.Cartesian3.fromDegrees(116.376, 39.895, 1600),
orientation: { heading: Cesium.Math.toRadians(30), pitch: Cesium.Math.toRadians(-28), roll: 0 }
},
{
destination: Cesium.Cartesian3.fromDegrees(116.389, 39.902, 60),
orientation: { heading: Cesium.Math.toRadians(0), pitch: Cesium.Math.toRadians(2), roll: 0 }
}
];
var buttons = [
document.getElementById('p0'),
document.getElementById('p1'),
document.getElementById('p2')
];
function select(i) {
viewer.camera.setView(POSES[i]);
buttons.forEach(function (b, k) { b.className = k === i ? 'on' : ''; });
document.getElementById('p3').className = '';
}
buttons.forEach(function (b, i) {
b.addEventListener('click', function () { select(i); });
});
/* lookAt:给出目标点、航向俯仰与距离,相机自动落在球面外 */
document.getElementById('p3').addEventListener('click', function () {
buttons.forEach(function (b) { b.className = ''; });
this.className = 'on';
viewer.camera.lookAt(
TARGET,
new Cesium.HeadingPitchRange(
Cesium.Math.toRadians(35),
Cesium.Math.toRadians(-18),
900
)
);
viewer.camera.lookAtTransform(Cesium.Matrix4.IDENTITY);
});
var camEl = document.getElementById('cam');
var carto = new Cesium.Cartographic();
scene.postRender.addEventListener(function () {
Cesium.Cartographic.fromCartesian(viewer.camera.positionWC, undefined, carto);
camEl.textContent = '高 ' + Math.round(carto.height) + ' 米 · 航向 ' +
Math.round(Cesium.Math.toDegrees(viewer.camera.heading)) + '°';
});
select(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.