1、viewer.scene.pick()
注意事项:只能获取一个对象,并且是最顶部的对象。如果拾取点没有对象,则会返回undefined
let handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);//处理用户输入事件
handler.setInputAction(function (event) { // 设置左键点击事件
let pick = viewer.scene.pick(event.position); // 获取 pick 拾取对象
if (Cesium.defined(pick)) { // 判断是否获取到了 pick
pick.id.billboard.image = "......" // 修改拾取到的entity的样式
}
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
2、viewer.scene.globe.pick()
**注意事项:**最好开启深度检测
let handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
handler.setInputAction(function (event) {
let ray = viewer.camera.getPickRay(event.position);//获取一条射线
let position = viewer.scene.globe.pick(ray, viewer.scene);
console.log("当前拾取的坐标:", position);
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
3、viewer.scene.camera.pickEllipsoid()
**注意事项:**在有地形的情况下误差较大,在使用时需要关闭深度测试。
let handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
handler.setInputAction(function (event) {
let position = viewer.scene.camera.pickEllipsoid(event.position, viewer.scene.globe.ellipsoid);
console.log("点击拾取的坐标:", position);
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
4、viewer.scene.pickPosition()
注意事项: 一定开启深度检测(viewer.scene.globe.depthTestAgainstTerrain = true
),否则在没有没有3dTile模型的情况下,会出现空间坐标不准的问题。
let handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
handler.setInputAction(function (event) {
let position = viewer.scene.pickPosition(event.position);
console.log("获取到的坐标:", positionposition);
}, Cesium.ScreenSpaceEventType.LEFT_CLICK)