CesiumJS 案例 P13:删除标记、移动标记、标记点击事件

CesiumJS


一、删除标记

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>
相关推荐
GISer_Jing9 分钟前
2025前端面试热门题目——计算机网络篇
前端·计算机网络·面试
m0_7482455210 分钟前
吉利前端、AI面试
前端·面试·职场和发展
理想不理想v22 分钟前
webpack最基础的配置
前端·webpack·node.js
pubuzhixing26 分钟前
开源白板新方案:Plait 同时支持 Angular 和 React 啦!
前端·开源·github
2401_8576009536 分钟前
SSM 与 Vue 共筑电脑测评系统:精准洞察电脑世界
前端·javascript·vue.js
2401_8576009536 分钟前
数字时代的医疗挂号变革:SSM+Vue 系统设计与实现之道
前端·javascript·vue.js
GDAL37 分钟前
vue入门教程:组件透传 Attributes
前端·javascript·vue.js
小白学大数据37 分钟前
如何使用Selenium处理JavaScript动态加载的内容?
大数据·javascript·爬虫·selenium·测试工具
2402_857583491 小时前
基于 SSM 框架的 Vue 电脑测评系统:照亮电脑品质之路
前端·javascript·vue.js
web150850966411 小时前
在uniapp Vue3版本中如何解决webH5网页浏览器跨域的问题
前端·uni-app