📌预览
📌代码
<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, PointGauger, } from "cesium-pocket/gauger";import { ElButton, ElMessageBox } from 'element-plus'import type { PointOptions } from "cesium-pocket/src/gauger/PointGauger.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 pointGauger: any = nullonMounted(() => { viewer.value = //你获取的viewer init()});
const isBtn1Disabled = () => { return pointGauger != 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 } pointGauger = new PointGauger(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 = () => { pointGauger.start() status.value = pointGauger.getStatus()}
const drawPointEdit = () => { pointGauger.setEditStatus() status.value = pointGauger.getStatus()}
const drawPointDelete = () => { pointGauger.setDelStatus() status.value = pointGauger.getStatus()}
const drawPointRemove = () => { pointGauger.clearAll() status.value = pointGauger.getStatus() pointGauger = null init()}.root-container { position: relative; width: 100wh; height: 100vh; overflow: hidden; }
.left-top-plugin { position: absolute; top: 20px; left: 10px; z-index: 10; }📌操作步骤及流程
- 点击“开始”进入绘制模式,可添加点,同时得到坐标
- 点击“编辑模式”进入编辑模式。鼠标变手型抓取时按住鼠标左键可拖拽节点,同时实时获取坐标,松开则放弃编辑。
- 点击“删除模式”,鼠标变手型时单击左键会弹出删除与否的提示,确认后删除。
- 点击“删除全部”可删除所有点。
- 可选择是否使用添加广告牌(billboard)的回调函数
- 可选择是否使用编辑后的回调函数
- 可自定义删除前的确认对话框
📌构造函数参数列表(new PointGauger(...))
| 序号 | 名称 | 类型 | 必须? | 默认值 | 含义 | 备注 |
|---|---|---|---|---|---|---|
| 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 | - | - | 清除所有对象 | - |