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>
相关推荐
三天不学习3 小时前
CSS 之 position 定位属性详解
前端·css·定位·position
亦可呀3 小时前
HTML-CSS-常见标签与样式
前端·css·html
web150850966414 小时前
【MsSQL】数据库基础 & 库的基本操作
前端·数据库·sqlserver
纳尼亚awsl5 小时前
处理元素卡在视野边界,滚动到视野内
前端·javascript·vue.js
黑客Jack5 小时前
XSS Challenges
前端·javascript·xss
黑客-秋凌5 小时前
XSS讲解
前端·xss
永远不会太晚5 小时前
JavaScript的diff库详解(示例:vue项目实现两段字符串比对标黄功能)
前端·javascript·vue.js
Json____5 小时前
网页单机版五子棋小游戏项目练习-初学前端可用于练习~
前端·javascript·css·html·五子棋·网页五子棋单机小程序
JM_life5 小时前
Python入门系列四-数据结构与算法基础
windows·python·html
lecepin6 小时前
前端技术月刊-2025.1
前端·javascript·面试