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>
相关推荐
布兰妮甜几秒前
Vue CLI: 安装、项目创建及基本概念指南,vue生命周期
前端·javascript·vue.js·前端框架
小呆i12 分钟前
Vue生成名片二维码带logo并支持下载
前端·javascript·vue.js
沃野_juededa27 分钟前
微信小程序app.js里面onLaunch里面的函数比page里面的onshow里面的方法后执行
javascript·微信小程序·小程序
tian-ming1 小时前
(五)Web前端开发进阶2——AJAX
前端
盒马盒马1 小时前
Ajax:跨域 & JSONP
前端·ajax
QEasyCloud20221 小时前
简道云和企业微信数据同步集成案例
java·大数据·前端
淡淡蛋痛1 小时前
D3.js 入门指南
开发语言·javascript·信息可视化
Cwhat1 小时前
通过自定义指令实现图片懒加载
前端·javascript·vue.js
WindrunnerMax1 小时前
Canvas简历编辑器-选中绘制与拖拽多选交互设计
前端·javascript·编辑器
小彭努力中2 小时前
102. 管道漫游案例
前端·3d·webgl