CesiumJS 案例 P18:检测文本、删除所有文本、隐藏与显示文本、改变文本

CesiumJS


一、检测文本

html 复制代码
<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<title>Label - 检测文本</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-text {
				position: fixed;
				left: 0px;
				top: 0px;
			}

			.btn-check-text {
				position: fixed;
				left: 0px;
				top: 50px;
			}
		</style>
	</head>

	<body>
		<div id="container"></div>
		<button class="btn-remove-text">删除文本</button>
		<button class="btn-check-text">检测文本</button>
	</body>

	<script src="../js/Cesium-1.112/Build/Cesium/Cesium.js"></script>
	<script>
		const viewer = new Cesium.Viewer("container");

		const labels = viewer.scene.primitives.add(new Cesium.LabelCollection());

		const label = labels.add({
			text: "Hello World",
			position: Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883),
		});

		const btnMoveText = document.querySelector(".btn-remove-text");
		const btnCheckText = document.querySelector(".btn-check-text");

		btnMoveText.addEventListener("click", () => {
			labels.remove(label);
		});

		btnCheckText.addEventListener("click", () => {
			console.log(labels.contains(label));
		});
	</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>Label - 删除文本</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 labels = viewer.scene.primitives.add(new Cesium.LabelCollection());

        labels.add({
            text: "Hello World",
            position: Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883),
        });

        labels.add({
            text: "Hello World",
            position: Cesium.Cartesian3.fromDegrees(-75.59777, 41.03883),
        });

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

        btnAllRemove.addEventListener("click", () => {
            labels.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>Label - 隐藏与显示文本</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 labels = viewer.scene.primitives.add(new Cesium.LabelCollection());

        const label = labels.add({
            text: "Hello World",
            position: Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883),
        });

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

        btnHideOrShow.addEventListener("click", () => {
            label.show = !label.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>Label - 改变文本</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-change {
                position: fixed;
                left: 0px;
                top: 0px;
            }
        </style>
    </head>

    <body>
        <div id="container"></div>
        <button class="btn-change">改变文本</button>
    </body>

    <script src="../js/Cesium-1.112/Build/Cesium/Cesium.js"></script>
    <script>
        const viewer = new Cesium.Viewer("container");

        const labels = viewer.scene.primitives.add(new Cesium.LabelCollection());

        const label = labels.add({
            text: "Hello World 1",
            position: Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883),
        });

        const btnChange = document.querySelector(".btn-change");

        btnChange.addEventListener("click", () => {
            label.text = "Hello World 2";
        });
    </script>
</html>
相关推荐
●VON几秒前
使用 Electron 构建天气桌面小工具:调用公开 API 实现跨平台实时天气查询V1.0.0
前端·javascript·electron·openharmony
码上成长3 分钟前
包管理提速:pnpm + Workspace + Changesets 搭建版本体系
前端·前端框架
Bigger7 分钟前
Tauri(十九)——实现 macOS 划词监控的完整实践
前端·rust·app
穷人小水滴43 分钟前
使用 epub 在手机快乐阅读
javascript·deno·科幻
ganshenml1 小时前
【Web】证书(SSL/TLS)与域名之间的关系:完整、通俗、可落地的讲解
前端·网络协议·ssl
这是个栗子2 小时前
npm报错 : 无法加载文件 npm.ps1,因为在此系统上禁止运行脚本
前端·npm·node.js
爱学习的程序媛2 小时前
《深入浅出Node.js》核心知识点梳理
javascript·node.js
HIT_Weston2 小时前
44、【Ubuntu】【Gitlab】拉出内网 Web 服务:http.server 分析(一)
前端·ubuntu·gitlab
华仔啊3 小时前
Vue3 如何实现图片懒加载?其实一个 Intersection Observer 就搞定了
前端·vue.js
JamesGosling6663 小时前
深入理解内容安全策略(CSP):原理、作用与实践指南
前端·浏览器