CesiumJS
-
CesiumJS API:https://cesium.com/learn/cesiumjs/ref-doc/index.html
-
CesiumJS 是一个开源的 JavaScript 库,它用于在网页中创建和控制 3D 地球仪(地图)
一、删除标记
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Billboard - 删除标记</title>
<link rel="stylesheet" href="../js/Cesium-1.112/Build/Cesium/Widgets/widgets.css" />
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
}
.container {
width: 100%;
height: 100%;
}
.btn-remove-marker {
position: fixed;
left: 0px;
top: 0px;
}
</style>
</head>
<body>
<div id="container"></div>
<button class="btn-remove-marker">删除标记</button>
</body>
<script src="../js/Cesium-1.112/Build/Cesium/Cesium.js"></script>
<script>
const viewer = new Cesium.Viewer("container");
const billboards = viewer.scene.primitives.add(new Cesium.BillboardCollection());
const billboard = billboards.add({
image: "../img/marker-icon.png",
position: Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883),
verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
});
const btnRemoveMarker = document.querySelector(".btn-remove-marker");
btnRemoveMarker.addEventListener("click", () => {
billboards.remove(billboard);
});
</script>
</html>
二、移动标记
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Billboard - 移动标记</title>
<link rel="stylesheet" href="../js/Cesium-1.112/Build/Cesium/Widgets/widgets.css" />
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
}
.container {
width: 100%;
height: 100%;
}
.btn-move-marker {
position: fixed;
left: 0px;
top: 0px;
}
</style>
</head>
<body>
<div id="container"></div>
<button class="btn-move-marker">移动标记</button>
</body>
<script src="../js/Cesium-1.112/Build/Cesium/Cesium.js"></script>
<script>
const viewer = new Cesium.Viewer("container");
const billboards = viewer.scene.primitives.add(new Cesium.BillboardCollection());
const billboard = billboards.add({
image: "../img/marker-icon.png",
position: Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883),
verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
});
const btnMoveMarker = document.querySelector(".btn-move-marker");
btnMoveMarker.addEventListener("click", () => {
billboard.position = Cesium.Cartesian3.fromDegrees(-75.59777, 50.03883);
});
</script>
</html>
三、标记点击事件
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Billboard - 标记点击事件</title>
<link rel="stylesheet" href="../js/Cesium-1.112/Build/Cesium/Widgets/widgets.css" />
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
}
.container {
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<div id="container"></div>
</body>
<script src="../js/Cesium-1.112/Build/Cesium/Cesium.js"></script>
<script>
const viewer = new Cesium.Viewer("container");
const billboards = viewer.scene.primitives.add(new Cesium.BillboardCollection());
const billboard = billboards.add({
id: "billboard-test",
image: "../img/marker-icon.png",
position: Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883),
verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
});
viewer.screenSpaceEventHandler.setInputAction((movement) => {
const pickedObject = viewer.scene.pick(movement.position);
if (Cesium.defined(pickedObject) && pickedObject.id === "billboard-test") {
console.log("点击了标记!!!");
} else {
console.log("未点击标记");
}
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
</script>
</html>