前言
缺少前置学习使用资料,请自行查阅:[https://blog.csdn.net/weixin_44402694/article/details/123110136](https://blog.csdn.net/weixin_44402694/article/details/123110136)
以下示例使用到的公共静态资料,不建议下载,建议官网自行下载超图Build资源,示例所涉及图片会在示例使用到时提供出来。如有需要可下载:[https://download.csdn.net/download/weixin_44402694/82180350](https://download.csdn.net/download/weixin_44402694/82180350)。
绘制面:通过Cesium.entities.add添加实体的方式绘制,根据已存在坐标绘制。
使用
-
效果
-
完整代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>绘制面 - 根据已知坐标绘制</title>
<link href="./public/Build/Cesium/Widgets/widgets.css" rel="stylesheet" />
<script
type="text/javascript"
src="./public/Build/Cesium/Cesium.js"
></script>
<style>
* {
margin: 0;
padding: 0;
}
html,
body,
#cesium-container {
width: 100%;
height: 100%;
}
.panel {
position: fixed;
left: 10px;
top: 10px;
z-index: 1;
background-color: #fff;
}
.panel .btn {
cursor: pointer;
}
.tooltip {
position: fixed;
left: -1000px;
top: -1000px;
background: rgba(0, 0, 0, 0.4);
padding: 4px;
border-radius: 4px;
display: none;
z-index: 1;
color: #fff;
}
</style>
</head>
<body>
<div id="cesium-container" />
<div class="panel">
<p class="btn">点击绘制面</p>
<p class="btn">点击清除面</p>
</div>
<script>
let viewer, line
const tooltip = document.querySelector('.tooltip')
window.onload = function () {
viewer = new Cesium.Viewer('cesium-container', {
navigation: false,
})
initDrawPointEventHandler()
}
// 点击按钮触发对应事件
function initDrawPointEventHandler() {
const btns = document.querySelectorAll('.panel .btn')
btns[0].addEventListener('click', () => {
clearLineHandler()
drawLineHandler()
})
btns[1].addEventListener('click', () => {
clearLineHandler()
})
}
// 根据坐标绘制线段
function drawLineHandler() {
line = viewer.entities.add({
id: 'test',
name: 'test',
polygon: {
hierarchy: [
{
x: 371300.6549514957,
y: 4218139.692113443,
z: 4764273.691342012,
},
{
x: -1735026.2553755913,
y: 3719033.6367810024,
z: 4877747.707789327,
},
{
x: -917885.7366678474,
y: 5248872.34113799,
z: 3501288.0342484456,
},
{
x: 371300.6549514957,
y: 4218139.692113443,
z: 4764273.691342012,
},
],
material: Cesium.Color.RED.withAlpha(0.5),
},
})
}
// 清除线段
function clearLineHandler() {
// viewer.entities.removeAll() // 清除场景中所有的entity实体
// viewer.entities.remove(line) // 传入指定的实体删除
viewer.entities.remove(viewer.entities.getById('test')) // 根据id获取到对应的实体再触发删除
}
</script>
</body>
</html>