CesiumJS
-
CesiumJS API:https://cesium.com/learn/cesiumjs/ref-doc/index.html
-
CesiumJS 是一个开源的 JavaScript 库,它用于在网页中创建和控制 3D 地球仪(地图)
一、添加文本
<!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%;
}
</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 labels = viewer.scene.primitives.add(new Cesium.LabelCollection());
const label = labels.add({
text: "Hello World",
position: Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883),
});
const entity = viewer.entities.add({
position: Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883),
point: {
pixelSize: 5,
color: new Cesium.Color(0, 1, 0, 1),
},
});
</script>
</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%;
}
</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 labels = viewer.scene.primitives.add(new Cesium.LabelCollection());
const label = labels.add({
text: "Hello World",
position: Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883),
font: "24px sans-serif",
horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
pixelOffset: new Cesium.Cartesian2(0, -75),
showBackground: true,
});
const entity = viewer.entities.add({
position: Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883),
point: {
pixelSize: 5,
color: new Cesium.Color(0, 1, 0, 1),
},
});
</script>
</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;
}
</style>
</head>
<body>
<div id="container"></div>
<button class="btn-remove-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 btnRemoveText = document.querySelector(".btn-remove-text");
btnRemoveText.addEventListener("click", () => {
labels.remove(label);
});
</script>
</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-move-text {
position: fixed;
left: 0px;
top: 0px;
}
</style>
</head>
<body>
<div id="container"></div>
<button class="btn-move-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-move-text");
btnMoveText.addEventListener("click", () => {
label.position = Cesium.Cartesian3.fromDegrees(-75.59777, 50.03883);
});
</script>
</html>
标签:const,100%,CesiumJS,add,P17,Cesium,new,文本
From: https://blog.csdn.net/weixin_52173250/article/details/143428702