Cesium拾取坐标三种方式(摘抄)

首先基于事件触发捕捉位置信息,一般是Cesium.ScreenSpaceEventType.MOUSE_MOVE,也可以是Cesium.ScreenSpaceEventType.LEFT_CLICK或者Cesium.ScreenSpaceEventType.RIGHT_CLICK

1
2
3
4
this.mouseHandler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
this.mouseHandler.setInputAction((movement) => {
// 添加方法
},Cesium.ScreenSpaceEventType.MOUSE_MOVE)

根据场景加载数据和需求,大部分情况下,我们拾取的是模型表面位置,所以使用方法一。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// 方式一: 从场景的深度缓冲区中拾取相应的位置,可以采集entity,primitive,3dtile
// 当拾取区域无entity,primitive,3dtile时,将返回一个无法预料的坐标(标准椭球面以下,无法使用,无法预料)
let cartesianPosition = viewer.scene.pickPosition(movement.position);
if (cartesian3) {
// 下面两个都行
// let cartographic3 = Cesium.Cartographic.fromCartesian(cartesianPosition);
let cartographic3 = ellipsoid.cartesianToCartographic(cartesianPosition);
this.pickInfoOption3.lng = Cesium.Math.toDegrees(cartographic3.longitude);
this.pickInfoOption3.lat = Cesium.Math.toDegrees(cartographic3.latitude);
this.pickInfoOption3.height = cartographic3.height;
}
// 方式二:获取当前点击视线与地球表面相交的坐标(有地形时候均可用,无地形时高程几乎为0)(无视entity、3dtile,不能拾取其表面坐标)
let ray = viewer.scene.camera.getPickRay(movement.position);
let cartesian1 = viewer.scene.globe.pick(ray, viewer.scene);
if (cartesian1) {
let cartographic1 = Cesium.Ellipsoid.WGS84.cartesianToCartographic(cartesian1);
this.pickInfoOption1.lng = Cesium.Math.toDegrees(cartographic1.longitude);
this.pickInfoOption1.lat = Cesium.Math.toDegrees(cartographic1.latitude);
this.pickInfoOption1.height = cartographic1.height;
}
// 方式三:获取当前点击视线与标准椭球面相交处的坐标,无高程(恒为0)
let cartesian2 = viewer.camera.pickEllipsoid(movement.position, ellipsoid);
if (cartesian2) {
let cartographic2 = ellipsoid.cartesianToCartographic(cartesian2);
this.pickInfoOption2.lng = Cesium.Math.toDegrees(cartographic2.longitude);
this.pickInfoOption2.lat = Cesium.Math.toDegrees(cartographic2.latitude);
this.pickInfoOption2.height = cartographic2.height;
}