CesiumJS 案例 P16:标记平移、删除标记、隐藏与显示标记、标记颜色

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%;
            }
        </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({
            image: "../img/marker-icon.png",
            position: Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883),
            verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
        });

        const startPosition = billboard.position.clone();
        const endPosition = Cesium.Cartesian3.fromDegrees(-75.60777, 45.03883);

        let intervalTime = 500;
        let nowTime = 0;
        let targetTime = 5 * 1000;

        interval = setInterval(() => {
            nowTime += intervalTime;
            billboard.position = Cesium.Cartesian3.lerp(startPosition, endPosition, nowTime / targetTime, new Cesium.Cartesian3());
            if (nowTime === targetTime) clearInterval(interval);
        }, intervalTime);
    </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-all-remove {
                position: fixed;
                left: 0px;
                top: 0px;
            }
        </style>
    </head>

    <body>
        <div id="container"></div>
        <button class="btn-all-remove">删除所有标记</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());

        billboards.add({
            image: "../img/marker-icon.png",
            position: Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883),
            verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
        });

        billboards.add({
            image: "../img/marker-icon.png",
            position: Cesium.Cartesian3.fromDegrees(-75.59777, 41.03883),
            verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
        });

        const btnAllRemove = document.querySelector(".btn-all-remove");

        btnAllRemove.addEventListener("click", () => {
            billboards.removeAll();
        });
    </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-hide-or-show {
                position: fixed;
                left: 0px;
                top: 0px;
            }
        </style>
    </head>

    <body>
        <div id="container"></div>
        <button class="btn-hide-or-show">隐藏 / 显示标记</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),
        });

        const btnHideOrShow = document.querySelector(".btn-hide-or-show");

        btnHideOrShow.addEventListener("click", () => {
            billboard.show = !billboard.show;
        });
    </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());

		// 不透明标记
		billboards.add({
			image: "../img/marker-icon.png",
			position: Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883),
		});

		// 半透明标记
		billboards.add({
			image: "../img/marker-icon.png",
			position: Cesium.Cartesian3.fromDegrees(-75.59777, 41.03883),
			color: new Cesium.Color(1.0, 1.0, 1.0, 0.5),
		});

		// 全透明标记
		billboards.add({
			image: "../img/marker-icon.png",
			position: Cesium.Cartesian3.fromDegrees(-75.59777, 42.03883),
			color: new Cesium.Color(1.0, 1.0, 1.0, 0),
		});
	</script>
</html>
相关推荐
敏编程2 分钟前
网页前端开发之HTML入门篇:表格标签 table
前端·html
&活在当下&5 分钟前
js 将文件 url 地址转成 Blob 文件流并下载
前端·javascript·vue.js
shix .7 分钟前
vue3中setup的作用是什么?
前端·javascript·vue.js
随笔写29 分钟前
JavaScript 读取及写入本地文件
开发语言·javascript·ecmascript
AIoT科技物语31 分钟前
免费,基于React + ECharts 国产开源 IoT 物联网 Web 可视化数据大屏
前端·物联网·react.js·开源·echarts
编程的幸37 分钟前
前端三件套(HTML + CSS + JS)
前端·css·html
挂科边缘1 小时前
基于YOLOv8 Web的安全帽佩戴识别检测系统的研究和设计,数据集+训练结果+Web源码
前端·人工智能·python·yolo·目标检测·计算机视觉
思茂信息1 小时前
CST汽车天线仿真(双向混合求解)
javascript·人工智能·5g·汽车·ar·软件工程
谢尔登2 小时前
【React】styled-components 基本使用
前端·javascript·css
王解2 小时前
Jest进阶知识:深入测试 React Hooks-确保自定义逻辑的可靠性
前端·javascript·react.js·typescript·单元测试·前端框架