📌预览
📌代码
<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="warning" plain @click="drawPolygonEdit" :disabled="isBtn2Disabled()">编辑模式</el-button> <el-button type="danger" plain @click="drawPolygonRemove" :disabled="isBtn4Disabled()">删除全部</el-button> </div> </div></template>import { onMounted, onUnmounted, ref } from "vue";import { Color, Viewer } from 'cesium';import { CircleDrawer, DRAW_STATUS, type CircleOptions, } from "cesium-pocket/drawer";import { ElButton, } from 'element-plus'
const rootContainer: any = ref(null);const viewer = ref<Viewer | null>(null)const status = ref(null)let polygonDrawer: any = null
onMounted(() => { viewer.value = //你获取的viewer init()});
onUnmounted(() => { if (viewer.value && !viewer.value.isDestroyed()) { viewer.value.destroy(); viewer.value = null; }});
const isBtn1Disabled = () => { return polygonDrawer != null && status.value != DRAW_STATUS.STOPED}
const isBtn2Disabled = () => { return status.value != DRAW_STATUS.DRAWABLE && status.value != DRAW_STATUS.DELETABLE}
const isBtn4Disabled = () => { return status.value != DRAW_STATUS.DRAWABLE && status.value != DRAW_STATUS.EDITABLE && status.value != DRAW_STATUS.DELETABLE}
const init = () => { let ellipseObj = { semiMinorAxis: 1.0, // 短半轴(米) semiMajorAxis: 1.0, // 长半轴(米)— 两者相等即为圆 material: Color.GREEN.withAlpha(0.4), // 填充颜色,半透明 height: 0, // 椭圆所在高度(米),可选 extrudedHeight: 0, // 拉伸高度,>0 会形成圆柱体,设为0则为平面 outline: true, // 是否显示边框 outlineColor: Color.GREEN }
let options: CircleOptions = { ellipse: ellipseObj, } polygonDrawer = new CircleDrawer(viewer.value as Viewer, options)}
const start = () => { polygonDrawer.start() status.value = polygonDrawer.getStatus()}
const drawPolygonEdit = () => { polygonDrawer.setEditStatus() status.value = polygonDrawer.getStatus()}
const drawPolygonRemove = () => { polygonDrawer.clearAll() status.value = polygonDrawer.getStatus() init()} .root-container { position: relative; width: 100wh; height: 100vh; overflow: hidden; }
.left-top-plugin { position: absolute; top: 20px; left: 10px; z-index: 10; }📌操作步骤及流程
- 点击“开始绘制”进入绘制模式,可绘制圆面
- 点击“编辑模式”进入编辑模式。鼠标变手型抓取时按住鼠标左键可拖拽节点,松开则放弃编辑。(拖拽圆心则移动圆面,拖拽边缘点则修改圆面半径)
- 点击“删除全部”可删除所有圆面。
- 可自定义节点和圆面的实体要素
📌构造函数参数列表(new CircleDrawer(...))
| 序号 | 名称 | 类型 | 必须? | 默认值 | 含义 | 备注 |
|---|---|---|---|---|---|---|
| 1 | viewer | Cesium.Viewer | 是 | - | viewer对象 | - |
| 2 | options{ ... } | CircleOptions | 否 | - | 其他参数 | 参数集对象 |
| 2-1 | options.point | Cesium.PointGraphics.ConstructorOptions | 否 | - | Entity的PointGraphics参数项 | - |
| 2-2 | options.ellipse | Cesium.EllipseGraphics.ConstructorOptions | 否 | - | Entity的EllipseGraphics参数项 | - |
| 2-3 | options.circleOverFun | (startPoint: Cartesian3, endPoint: Cartesian3) => void | 否 | - | 绘制结束时的回调函数 | - |
| 2-4 | options.circleMovingFun | (startPoint: Cartesian3, endPoint: Cartesian3) => void | 否 | - | 绘制移动时的回调函数 | - |
| 2-5 | options.circleEditFun | () => Promise<boolean> | 否 | {浏览器自带confirm} | 删除时待确认的回调函数 | - |
📌API方法列表
| 序号 | 方法名 | 入参 | 返回值 | 含义 | 备注 |
|---|---|---|---|---|---|
| 1 | start | - | void | 开启绘制圆面 | - |
| 2 | getEntity | - | Cesium.Entity | 获得圆面Entity | - |
| 3 | getDataSource | - | Cesium.DataSource | 获得点所有节点的DataSource | - |
| 4 | getPositions | - | Cartain3[] | 获得点所有节点的坐标 | - |
| 5 | clearAll | - | - | 清除所有对象 | - |