三维标绘、量算、位置拾取、拖拽与模型位置调整。下面是该示例的核心 script 片段,供参考学习;完整可运行代码请进入在线 Demo 或查看仓库源码。
import * as Cesium from 'cesium'
import { initCesium } from '@/utils/cesiumPluginsExtends/index'
let cHander = null;
export default {
data () {
return {
posHeading: 0,
posPitch: 0,
posRoll: 0,
posAlt: 0,
posLat: 0,
posLon: 0,
cartesinaX: 0,
cartesinaY: 0,
cartesinaZ: 0,
cartesinaH: 0,
cartesinaP: 0,
cartesinaR: 0
}
},
mounted () {
this.initMap()
},
methods: {
async initMap () {
const tempData = [
{
type: 'UrlTemplateImageryProvider',
option: {
url: 'https://webst02.is.autonavi.com/appmaptile?style=6&x={x}&y={y}&z={z}'
}
},
{
type: 'UrlTemplateImageryProvider',
option: {
url: 'https://webst03.is.autonavi.com/appmaptile?x={x}&y={y}&z={z}&style=7',
}
}
]
const { viewer,
base
} = new initCesium(
{
cesiumGlobal: Cesium,
containerId: 'cesiumContainer',
viewerConfig: {
infoBox: false,
shouldAnimate: true,
},
extraConfig: {
depthTest: true // 深度测试
},
MapImageryList: tempData
})
this.c_viewer = viewer;
this.base = base;
let tiles = await Cesium.Cesium3DTileset.fromUrl('static/data/3DTiles/building/tileset.json');
let tileset = this.c_viewer.scene.primitives.add(tiles)
tileset.style = new Cesium.Cesium3DTileStyle({
color: {
conditions: [
["${height} >= 300", "rgba(0, 149, 251, 0.3)"],
["${height} >= 200", "rgb(0, 149, 251, 0.3)"],
["${height} >= 100", "rgb(0, 149, 251, 0.3)"],
["${height} >= 50", "rgb(0, 149, 251, 0.3)"],
["${height} >= 25", "rgb(0, 149, 251, 0.3)"],
["${height} >= 10", "rgb(0, 149, 251, 0.3)"],
["${height} >= 5", "rgb(0, 149, 251, 0.3)"],
["true", "rgb(0, 149, 251, 0.3)"]
]
}
});
viewer.flyTo(tileset)
this.initCameraPos();
this.cameraModify();
},
initCameraPos () {
let position = this.base.getCameraPosition();
if (position) {
const { lon,
lat,
height,
heading,
pitch,
roll,
position: { x, y, z },
cameraHeading,
cameraPitch,
cameraRoll,
} = position;
this.posAlt = height;
this.posHeading = heading;
this.posPitch = pitch;
this.posRoll = roll;
this.posLat = lat;
this.posLon = lon;
this.cartesinaX = x;
this.cartesinaY = y;
this.cartesinaZ = z;
this.cartesinaH = cameraHeading;
this.cartesinaP = cameraPitch;
this.cartesinaR = cameraRoll;
} else {
console.log('can not get postion.')
}
},
cameraModify () {
this.c_viewer.scene.camera.moveEnd.addEventListener((move) => {
let position = this.base.getCameraPosition()
if (position) {
const { lon,
lat,
height,
heading,
pitch,
roll,
position: { x, y, z },
cameraHeading,
cameraPitch,
cameraRoll,
} = position;
this.posAlt = height;
this.posHeading = heading;
this.posPitch = pitch;
this.posRoll = roll;
this.posLat = lat;
this.posLon = lon;
this.cartesinaX = x;
this.cartesinaY = y;
this.cartesinaZ = z;
this.cartesinaH = cameraHeading;
this.cartesinaP = cameraPitch;
this.cartesinaR = cameraRoll;
} else {
console.log('can not get postion.')
}
});
},
// 绑定事件到场景中
bindEventToScene (callbackFn) {
cHander && cHander.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
this.base.bindHandelEvent({
leftClick: (clickEvent, hander) => {
cHander = hander;
callbackFn && callbackFn(clickEvent)
}
})
},
/**
* 按钮组操作事件
* 如果获取所有位置点可以使用
* base.getCatesian3FromPX(e.position);
* @param {*} type
*/
btnGroupOpera (type) {
const scene = this.c_viewer.scene;
const _self = this;
if (type === 'scenePickBuild') {
this.bindEventToScene(e => {
// 拾取三维场景的物体
let pickedFeature = scene.pick(e.position);
if (!Cesium.defined(pickedFeature)) {
this.$message('nothing picked!')
console.log("nothing picked!")
} else {
this.$message('拾取到模型==》请控制台查看')
console.log("this is obation build=>", pickedFeature)
}
})
} else if (type === 'scenePickPos') {
this.bindEventToScene(e => {
// 用来拾取三维空间笛卡尔坐标
let cartesian = scene.pickPosition(e.position);
this.c_viewer.entities.add({
position: cartesian,
point: {
color: Cesium.Color.RED,
pixelSize: 50,// 像素大小
outlineColor: Cesium.Color.YELLOWGREEN,
outlineWidth: 2,// 边框
// heightReference: Cesium.HeightReference.CLAMP_TO_GROUND
}
})
})
} else if (type === 'drillPick') {
this.bindEventToScene(e => {
// 用来拾取三维空间物体
let pickedFeature = scene.drillPick(e.position, 2);
this.$message('拾取到模型==》请控制台查看')
console.log("this is obation build=>", pickedFeature)
})
} else if (type === 'PickElliposid') {
this.bindEventToScene(e => {
// 与球面求交,获取点击的球面位置
let pickPositionElliposid = scene.camera.pickEllipsoid(e.position, scene.globe.elliposid);
this.c_viewer.entities.add({
position: pickPositionElliposid,
point: {
color: Cesium.Color.RED,
pixelSize: 50,// 像素大小
outlineColor: Cesium.Color.YELLOWGREEN,
outlineWidth: 2,// 边框
heightReference: Cesium.HeightReference.CLAMP_TO_GROUND
}
})
})
} else if (type === 'globePick') {
this.bindEventToScene(e => {
let ray = this.c_viewer.camera.getPickRay(e.position);//创建一条于地球的射线
// 与地球globe求交,获取点击的球面位置
let globePickPos = scene.globe.pick(ray, scene);
// 将笛卡尔转为经纬度
let lonlat = Cesium.Cartographic.fromCartesian(globePickPos);
console.log('lon---lonlat', lonlat)
this.c_viewer.entities.add({
position: globePickPos,
point: {
color: Cesium.Color.RED,
pixelSize: 50,// 像素大小
outlineColor: Cesium.Color.YELLOWGREEN,
outlineWidth: 2,// 边框
heightReference: Cesium.HeightReference.CLAMP_TO_GROUND
}
})
})
} else if (type === 'getImg') {
this.bindEventToScene(e => {
// 获取img
let pickRayImg = this.c_viewer.camera.getPickRay(e.position);
let fePromise = this.c_viewer.imageryLayers.pickImageryLayerFeatures(pickRayImg, scene)
if (!Cesium.defined(fePromise)) {
this.$message('nothing picked!')
console.log("nothing picked!")
} else {
Cesium.when(fePromise, function (fea) {
// 获取到突出
if (fea.length > 0) {
_self.$message('拾取到模型==》请控制台查看')
console.log("this is obation build=>", fea)
} else {
_self.$message('未拾取到物体')
}
})
}
})
}
}
},
beforeUnmount () {
this.c_viewer = null;
this.base = null;
}
}
安装:npm install cesium_dev_kit cesium