cesium获取点击位置坐标
方式一:
let handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
handler.setInputAction(function (movement) {
var cartesian = viewer.camera.pickEllipsoid(
movement.position,
viewer.scene.globe.ellipsoid
);
var cartographic = Cesium.Cartographic.fromCartesian(cartesian);
console.log('lng',Cesium.Math.toDegrees(cartographic.longitude))
console.log('lat',Cesium.Math.toDegrees(cartographic.latitude))
},Cesium.ScreenSpaceEventType.LEFT_CLICK)
该方式无法获取点的高度。所以在绘制Entity时,Entity贴地需要设置heightReference: Cesium.HeightReference.CLAMP_TO_GROUND 。
const pinBuilder = new Cesium.PinBuilder();
var currEntity = new Cesium.Entity({
id: "current",
position: position,
billboard: {
image: pinBuilder.fromText("点", Cesium.Color.BLUE, 48).toDataURL(),
verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
scale: 0.8,
heightReference: Cesium.HeightReference.CLAMP_TO_GROUND
},
});
viewer.entities.add(currEntity);
而且即便设置了贴地,点击位置和绘制Entity的位置还是会出现不一致情况。如需在点击位置绘制,需要使用以下方式二。
方式二
let handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
handler.setInputAction(function (movement) {
var ray=viewer.camera.getPickRay(movement.position);
var cartesian= viewer.scene.globe.pick(ray, viewer.scene);
var cartographic = Cesium.Cartographic.fromCartesian(cartesian);
console.log('lng',Cesium.Math.toDegrees(cartographic.longitude))
console.log('lat',Cesium.Math.toDegrees(cartographic.latitude))
console.log('height',cartographic.height)
},Cesium.ScreenSpaceEventType.LEFT_CLICK)
//在绘制entity时也不需要使用贴地了
网友评论