📌预览

📌代码


<template>
<div id="root-container" ref="rootContainer" class="root-container">
<div class="left-top-plugin">
<el-button type="primary" plain @click="start" :disabled="isBtn1Disabled()">开始
</el-button>
<el-button type="primary" plain @click="drawPointEdit" :disabled="isBtn2Disabled()">进入编辑模式</el-button>
<el-button type="warning" plain @click="drawPointDelete" :disabled="isBtn3Disabled()">进入删除模式</el-button>
<el-button type="danger" plain @click="drawPointRemove" :disabled="isBtn4Disabled()">删除全部</el-button>
</div>
</div>
</template>
import { onMounted, onUnmounted, ref, } from "vue";
import { Cartesian2, Cartesian3, Color, LabelStyle, VerticalOrigin, Viewer } from 'cesium';
import { DRAW_STATUS, PointDrawer, } from "cesium-pocket/drawer";
import { ElButton, ElMessageBox } from 'element-plus'
import type { PointOptions } from "cesium-pocket/src/drawer/PointDrawer.js";
import stickRedPng from '@/assets/images/stick_red.png'
const rootContainer: any = ref(null);
const viewer = ref<Viewer | null>(null)
const status = ref(null)
let pointDrawer: any = null
onMounted(() => {
viewer.value = //你获取的viewer
init()
});
const isBtn1Disabled = () => {
return pointDrawer != null && status.value != DRAW_STATUS.STOPED
}
const isBtn2Disabled = () => {
return status.value != DRAW_STATUS.DRAWABLE && status.value != DRAW_STATUS.DELETABLE
}
const isBtn3Disabled = () => {
return status.value != DRAW_STATUS.DRAWABLE && status.value != DRAW_STATUS.EDITABLE
}
const isBtn4Disabled = () => {
return status.value != DRAW_STATUS.DRAWABLE && status.value != DRAW_STATUS.EDITABLE && status.value != DRAW_STATUS.DELETABLE
}
onUnmounted(() => {
if (viewer.value && !viewer.value.isDestroyed()) {
viewer.value.destroy();
viewer.value = null;
}
});
const init = () => {
let options: PointOptions = {
billboardCallbackFun: billboardCallbackFun,
delCallbackFun: delCallbackFun
}
pointDrawer = new PointDrawer(viewer.value as Viewer, options)
}
const billboardCallbackFun = (cartesian: Cartesian3) => {
const entity = (viewer.value as Viewer).entities.add({
// 设置经纬度位置 (经度, 纬度, 海拔高度)
position: cartesian,
label: {
text: '标注',
font: '500 14px Helvetica',
style: LabelStyle.FILL,
fillColor: Color.WHITE,
pixelOffset: new Cartesian2(0, -120), //偏移量
showBackground: true,
backgroundColor: new Color(254, 0, 0, 0.7)
},
billboard: {
image: stickRedPng,
// 设置图片显示的宽度和高度 (像素)
width: 30,
height: 100,
// 可选项:缩放倍数
scale: 1.0,
verticalOrigin: VerticalOrigin.BOTTOM,
},
properties: {
entityType: 'LOCATION'
},
show: true
});
return entity
}
const delCallbackFun = (): Promise<any> => {
return new Promise((resolve) => {
ElMessageBox.confirm(
'确定删除该节点吗?',
'',
{
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning',
}
).then(() => {
resolve(true)
}).catch(() => {
resolve(false)
})
})
}
const start = () => {
pointDrawer.start()
status.value = pointDrawer.getStatus()
}
const drawPointEdit = () => {
pointDrawer.setEditStatus()
status.value = pointDrawer.getStatus()
}
const drawPointDelete = () => {
pointDrawer.setDelStatus()
status.value = pointDrawer.getStatus()
}
const drawPointRemove = () => {
pointDrawer.clearAll()
status.value = pointDrawer.getStatus()
pointDrawer = null
init()
}
.root-container {
position: relative;
width: 100wh;
height: 100vh;
overflow: hidden;
}
.left-top-plugin {
position: absolute;
top: 20px;
left: 10px;
z-index: 10;
}

📌操作步骤及流程

  1. 点击“开始绘制”进入绘制模式,可绘制点
  2. 点击“编辑模式”进入编辑模式。鼠标变手型抓取时按住鼠标左键可拖拽节点,松开则放弃编辑。
  3. 点击“删除模式”,鼠标变手型时单击左键会弹出删除与否的提示,确认后删除。
  4. 点击“删除全部”可删除所有点。
  5. 可选择是否使用添加广告牌(billboard)的回调函数
  6. 可选择是否使用编辑后的回调函数
  7. 可自定义删除前的确认对话框

📌构造函数参数列表(new PointDrawer(...))

序号 名称 类型 必须? 默认值 含义 备注
1 viewer Cesium.Viewer - viewer对象 -
2 options{ ... } PointOptions - 其他参数 参数集对象
2-1 options.addPointFun (cartesian: Cartesian3) => Entity (绿色定位图标) 添加点时的回调函数 -
2-2 options.afterAddPointFun (cartesian: Cartesian3) => void - 添加点后的回调函数 -
2-3 options.editPointFun (cartesian: Cartesian3, entity: Entity) => void - 编辑结束后的回调函数 -
2-4 options.delConfirmFun () => Promise<boolean> {浏览器自带confirm} 删除时待确认的回调函数 -

📌API方法列表

序号 方法名 入参 返回值 含义 备注
1 start - void 开启绘制点 -
2 getEntities - Cesium.EntityCollection 所有点的集合 -
3 getDataSource - Cesium.DataSource 获得点所有节点的DataSource -
4 clearAll - - 清除所有对象 -