?这个是根据鼠标点击的是地形还是模型,动态识别获取对应目标上的经纬度和高程数据,window.mapViewer则是创建的Cesium地图对象;
getPoint() {
let _this = this;
this.handler = new Cesium.ScreenSpaceEventHandler(
window.mapViewer.scene.canvas
);
this.handler.setInputAction(function (clickEvent) {
let mapPosition = {};
let position;
if (clickEvent.position) {
position = clickEvent.position;
} else {
position = clickEvent.endPosition;
}
let pick = window.mapViewer.scene.pickPosition(position);
// 获取模型对象
let pickModel = window.mapViewer.scene.pick(position);
// 如果点击的是模型
if (pickModel && pick) {
// 笛卡尔坐标系转为经纬度(弧度)坐标
let cartographic = Cesium.Cartographic.fromCartesian(pick);
// 再将经纬度(弧度)坐标转化为经纬度角度坐标
let lat = Cesium.Math.toDegrees(cartographic.latitude);
let lng = Cesium.Math.toDegrees(cartographic.longitude);
mapPosition = {
x: lng,
y: lat,
z: cartographic.height + 3,
description: "模型坐标",
};
} else {
let ray = window.mapViewer.camera.getPickRay(position);
let cartesian = window.mapViewer.scene.globe.pick(
ray,
window.mapViewer.scene
);
// 笛卡尔坐标系转为经纬度(弧度)坐标
let cartographic = Cesium.Cartographic.fromCartesian(cartesian);
// 再将经纬度(弧度)坐标转化为经纬度角度坐标
let lng = Cesium.Math.toDegrees(cartographic.longitude);
let lat = Cesium.Math.toDegrees(cartographic.latitude);
mapPosition = {
x: lng,
y: lat,
z: cartographic.height + 3,
description: "地形坐标",
};
}
_this.points.push(mapPosition);
console.log("鼠标绘制的点", _this.points);
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
},