应用说明
- 添加静态图到指定
extent
- 通过键盘事件对
static image
进行调整
- 向东、西、南、北移动
- 沿东西、南北方向缩放
- 命令行侧输出当前
extent
,可用于几何匹配
初始化
- Earth 初始化
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<!-- Include the CesiumJS JavaScript and CSS files -->
<script src="https://cesium.com/downloads/cesiumjs/releases/1.111/Build/Cesium/Cesium.js"></script>
<link href="https://cesium.com/downloads/cesiumjs/releases/1.111/Build/Cesium/Widgets/widgets.css" rel="stylesheet">
<style>
html, body{ margin:0; height:100%; }
#cesiumContainer {
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<div id="cesiumContainer"></div>
<script>
Cesium.Ion.defaultAccessToken = 'your_key';
const viewer = new Cesium.Viewer('cesiumContainer',
{
geocoder: true,
homeButton: false,
sceneModePicker: false,
baseLayerPicker: false,
baseLayer: false,
navigationHelpButton: false,
animation: false,
shouldAnimate: false,
timeline: false,
fullscreenButton: false,
selectionIndicator: false,
infoBox: false,
});
viewer._cesiumWidget._creditContainer.style.display = "none";
</script>
</div>
</body>
</html>
2. 添加OSM底图
viewer.imageryLayers.addImageryProvider(
new Cesium.OpenStreetMapImageryProvider()
)
添加静态图
- 配置初始参数
![
const extent = \[115.5141293319635, 116.44787066803651, 38.67539196000001, 39.21660804000001\]
let west = extent\[0\], east = extent\[1\], south = extent\[2\], north = extent\[3\]
let latDelta = 0.001, lonDelta = 0.001, latScale = 0.01, lonScale = 0.01;](https://img-blog.csdnimg.cn/direct/5b604d9cb241470781800c153e7a9434.png)
- 创建静态图片材质和对应primitive
const imageMaterial = new Cesium.Material({
fabric: {
type: 'Image',
uniforms: {
image: "../../SampleData/10236.jpg"
},
components: {
alpha: 0.5,
},
}
});
function getPrimitive(){
return new Cesium.Primitive({
geometryInstances: new Cesium.GeometryInstance({
geometry: new Cesium.RectangleGeometry({
rectangle: Cesium.Rectangle.fromDegrees(
west,
south,
east,
north
),
vertexFormat: Cesium.EllipsoidSurfaceAppearance.VERTEX_FORMAT,
}),
}),
appearance: new Cesium.EllipsoidSurfaceAppearance({
material: imageMaterial,
aboveGround: false,
}),
})
}
- 添加primitive并flyTo
let staticImagePrimitive = viewer.scene.primitives.add(
getPrimitive()
);
viewer.camera.flyTo({
destination : Cesium.Rectangle.fromDegrees(west, south, east, north)
});
添加事件响应
- 开启键盘事件响应
const scene = viewer.scene;
const canvas = viewer.canvas;
canvas.setAttribute("tabindex", "0");
canvas.onclick = function () {
canvas.focus();
};
- 对键盘事件进行解析
function getFlagForKeyCode(keyCode) {
switch (keyCode) {
case "W".charCodeAt(0):
return "moveUp";
case "S".charCodeAt(0):
return "moveDown";
case "Q".charCodeAt(0):
return "xScale";
case "E".charCodeAt(0):
return "yScale";
case "Z".charCodeAt(0):
return "xDownScale";
case "C".charCodeAt(0):
return "yDownScale";
case "D".charCodeAt(0):
return "moveRight";
case "A".charCodeAt(0):
return "moveLeft";
default:
return undefined;
}
}
- 添加监听事件
document.addEventListener(
"keydown",
function (e) {
const flagName = getFlagForKeyCode(e.keyCode);
if (flagName == "moveUp"){
north = north + latDelta;
south = south + latDelta;
}
else if (flagName == "moveDown"){
north = north - latDelta;
south = south - latDelta;
}
else if (flagName == "moveRight"){
west = west + lonDelta;
east = east + lonDelta;
}
else if (flagName == "moveLeft"){
west = west - lonDelta;
east = east - lonDelta;
}
else if (flagName == "xScale"){
let xDelta = (east - west) * lonScale;
west = west - xDelta;
east = east + xDelta;
}
else if (flagName == "yScale"){
let yDelta = (north - south) * latScale;
north = north + yDelta;
south = south - yDelta;
}
else if (flagName == "xDownScale"){
let xDelta = (east - west) * lonScale;
west = west + xDelta;
east = east - xDelta;
}
else if (flagName == "yDownScale"){
let yDelta = (north - south) * latScale;
south = south + yDelta;
north = north - yDelta;
} else {
return
}
updatePrimitive(staticImagePrimitive)
console.log(west, east, south, north)
}
);
function updatePrimitive(primitive){
viewer.scene.primitives.remove(primitive)
staticImagePrimitive = viewer.scene.primitives.add(
getPrimitive()
);
}