三维标绘、量算、位置拾取、拖拽与模型位置调整。下面是该示例的核心 script 片段,供参考学习;完整可运行代码请进入在线 Demo 或查看仓库源码。
import * as Cesium from 'cesium'
import { initCesium } from '@/utils/cesiumPluginsExtends/index'
let ctrlManualFlag = {
selected: false,
leftDown: false
}
export default {
data() {
return {
activeId: 'move',
plotEntitiesId: [],
menuData: [
{
label: '平移',
value: 'move'
},
{
label: '旋转',
value: 'rotate'
},
{
label: '缩放',
value: 'scale'
}]
}
},
mounted() {
this.initMap()
},
methods: {
initMap() {
const { viewer, graphics, control } = new initCesium(
{
cesiumGlobal: Cesium,
containerId: 'cesiumContainer',
viewerConfig: {
infoBox: false,
shouldAnimate: true,
},
extraConfig: {},
MapImageryList: []
})
this.c_viewer = viewer;
this.graphics = graphics;
this.control = control;
this.initModel(viewer);
},
initModel(viewer) {
const entity = this.graphics.createBoxGraphics({
position: Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883, 0.0),
name: "box",
box: {
dimensions: new Cesium.Cartesian3(500.0, 500.0, 500.0),//尺寸,长宽高
material: new Cesium.ColorMaterialProperty((new Cesium.CallbackProperty(() => {
if (ctrlManualFlag.selected && ctrlManualFlag.leftDown) {
return Cesium.Color.GREENYELLOW.withAlpha(0.5)
}
return Cesium.Color.WHITE;
}, false))),
}
})
viewer.flyTo(entity)
this.initMouseEvent(entity)
},
initMouseEvent(entity) {
this.control.definedMouseEvent({ targetEntity: entity, ctrlFlag: ctrlManualFlag })
},
caldDistain(item) {
this.activeId = item.value
switch (item.value) {
case 'move':
this.control.manualMoveModel(item.value);
break
case 'rotate':
this.control.manualRotateModel(item.value);
break
case 'scale':
this.control.manualScaleModel(item.value);
break
}
},
},
beforeUnmount() {
// 清除绑定的事件
this.graphics.removeHandlerByName(['LEFT_CLICK', 'MOUSE_MOVE', 'LEFT_DOWN', 'LEFT_UP', 'WHEEL']);
this.c_viewer = null;
this.graphics = null;
}
}
安装:npm install cesium_dev_kit cesium