📌效果预览
📌操作步骤及流程
- 点击“开始”进入绘制模式,可绘制折线,同时计算每一段的距离
- 点击“进入编辑模式”开始编辑模式。鼠标变手型抓取时按住鼠标左键可拖拽节点,松开则放弃编辑。编辑时每段距离及总距离实时更新
- 点击“进入删除模式”,鼠标变手型时单击左键会弹出删除与否的提示,确认后删除。删除后新的分段距离及总距离更新
- 可自定义节点、实线和虚线的实体要素
- 可自定义删除前的确认对话框
- 点击“删除全部”可删除所有折线。
📌核心代码
<template> <div id="root-container" ref="rootContainer" class="root-container"> <div class="left-top-plugin"> <el-button type="primary" plain @click="start" :disabled="started">开始 </el-button> <el-button type="primary" plain @click="drawLineEdit" :disabled="!started || edited">进入编辑模式</el-button> <el-button type="warning" plain @click="drawLineDelete" :disabled="!started || deleted">进入删除模式</el-button> <el-button type="danger" plain @click="drawLineRemove" :disabled="!started">删除全部</el-button> </div> </div></template>import { onMounted, onUnmounted, ref, } from "vue";import { PocketViewer } from '@/util/PocketViewer'import { Viewer } from 'cesium';import { LineGaguger, type LineGaugerOptions } from "cesium-pocket/gauger";import { ElButton, ElMessageBox } from 'element-plus'
const rootContainer: any = ref(null);const viewer = ref<Viewer | null>(null)const status = ref(null)const started = ref(false)const edited = ref(false)const deleted = ref(false)let lineGauger: any = null
onMounted(() => { let mfCesium = new PocketViewer(rootContainer.value, {}) viewer.value = mfCesium.getViewer() init()});
onUnmounted(() => { if (viewer.value && !viewer.value.isDestroyed()) { viewer.value.destroy(); viewer.value = null; }});
let delConfirmFun = (): Promise<any> => { return new Promise((resolve) => { ElMessageBox.confirm( '确定删除该节点吗?', '', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning', } ).then(() => { resolve(true) }).catch(() => { resolve(false) }) })}
const init = () => { let options: LineGaugerOptions = { delConfirmFun: delConfirmFun } lineGauger = new LineGaguger(viewer.value as Viewer, options)}
const start = () => { lineGauger.start() started.value = true}
const drawLineEdit = () => { lineGauger.setEditStatus() edited.value = true deleted.value = false}
const drawLineDelete = () => { lineGauger.setDelStatus() deleted.value = true edited.value = false}
const drawLineRemove = () => { lineGauger.clearAll() status.value = lineGauger.getStatus() started.value = false edited.value = false deleted.value = false init()}.root-container { position: relative; width: 100wh; height: 100vh; overflow: hidden; }
.left-top-plugin { position: absolute; top: 20px; left: 10px; z-index: 10; }📌构造函数参数列表(new LineGauger(...))
| 序号 | 名称 | 类型 | 必须? | 默认值 | 含义 | 备注 |
|---|---|---|---|---|---|---|
| 1 | viewer | Cesium.Viewer | 是 | - | viewer对象 | - |
| 2 | options{ ... } | CP2dOptions | 否 | - | 其他参数 | 参数集对象 |
| 2-1 | options.gaugerMode | DRAWER_MODE_TYPE | 否 | DRAWER_MODE.LINE | 绘制模式(线or面) | DRAWER_MODE.LINE,DRAWER_MODE.POLYGON |
| 2-2 | options.point | Cesium.PointGraphics.ConstructorOptions | 否 | {黄色空心圆} | Entity的PointGraphics参数项 | - |
| 2-3 | options.polyline | Cesium.PolylineGraphics.ConstructorOptions | 否 | {红色实线} | Entity的PolylineGraphics参数项 | - |
| 2-4 | options.deshline | Cesium.PolylineGraphics.ConstructorOptions | 否 | {红色虚线} | Entity的PolylineGraphics参数项 | - |
| 2-5 | options.afterAddPointFun | (cartesian: Cartesian3) => void | 否 | {浏览器自带confirm} | 删除时的回调函数 | - |
| 2-6 | options.movingCallbackFun | (cartesian: Cartesian3) => void | 否 | - | 绘制移动时的回调函数 | options参数 |
| 2-7 | options.editCallbackFun | (cartesian: Cartesian3, index: number) => void | 否 | - | 绘制编辑拖拽时的回调函数 | options参数 |
| 2-8 | options.delCallbackFun | () => void | 否 | - | 删除节点后的回调函数 | - |
| 2-9 | options.delConfirmFun | () => Promise<boolean> | 否 | {浏览器自带confirm} | 删除时待确认的回调函数 | - |
| 2-10 | options.overCallbackFun | () => void | 否 | - | 绘制结束时的回调函数 | - |
📌API方法列表
| 序号 | 方法名 | 入参 | 返回值 | 含义 | 备注 |
|---|---|---|---|---|---|
| 1 | start | - | void | 开启绘制线 | - |
| 2 | getEntity | - | Cesium.Entity | 获得折线的Entity | - |
| 3 | getDataSource | - | Cesium.DataSource | 获得点所有节点的DataSource | - |
| 4 | getPositions | - | Cartain3[] | 获得点所有节点的坐标 | - |
| 5 | clearAll | - | - | 清除所有对象 | - |