关于cesium获取鼠标监听事件,并实现点击地图对应位置打印经纬度高度信息

该代码示例展示了如何利用Cesium库中的ScreenSpaceEventHandler实例处理用户输入,特别是鼠标左键点击事件。当用户左键点击时,它获取并打印出当前位置的经度、纬度和高度。此外,还提到了其他可用的ScreenSpaceEventType以及如何设置输入事件的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

function bindEvent() {
  var drawHandler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
  drawHandler.setInputAction(click => {
    //查看当前视角的 x,y,z,heading,pitch,roll值
    var e = click;
    var position = viewer.scene.pickPosition(e.position);
    //将笛卡尔坐标转化为经纬度坐标
    var cartographic = Cesium.Cartographic.fromCartesian(position);
    var x = Cesium.Math.toDegrees(cartographic.longitude);
    var y = Cesium.Math.toDegrees(cartographic.latitude);
    var z = cartographic.height;
    console.log(x, y, z);
    var pickModel = viewer.scene.pick(click.position);
  }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
}

主要用到的时cesium对象下的ScreenSpaceEventHandler实例的setInputAction方法,官方文档对于该对象的解释如下:

new Cesium.ScreenSpaceEventHandler ( element )

处理用户输入事件。可以添加自定义功能以在以下位置执行当用户输入输出时。

NameTypeDefaultDescription
elementHTMLCanvasElementdocument 可选要向其中添加事件的元素。

添加需要绑定的事件的方法,还有其他的方法不一一介绍了,有兴趣的可以参考官方文档

setInputAction (action, type, modifier )

设置要在输入事件上执行的功能。

NameTypeDescription
actionfunction输入事件发生时要执行的功能。
typeNumber输入事件的ScreenSpaceEventType。
modifierNumber可选当 type 时按住的KeyboardEventModifier键事件发生。

其中type(ScreenSpaceEventType可用的事件有

名称 描述
LEFT_DOWN  Represents a mouse left button down event.表示鼠标左键按下事件。
LEFT_UPRepresents a mouse left button up event.表示鼠标左键弹起事件。
LEFT_CLICK  Represents a mouse left click event.表示鼠标左键点击事件。
LEFT_DOUBLE_CLICK  Represents a mouse left double click event.表示鼠标左键双击事件。
RIGHT_DOWN  Represents a mouse left button down event.表示鼠标右键按下事件。
RIGHT_UPRepresents a mouse right button up event.表示鼠标右键弹起事件。
RIGHT_CLICK  Represents a mouse right click event.表示鼠标右键单击事件。
MIDDLE_DOWN  Represents a mouse middle button down event.表示鼠标中键按下事件。
  
MIDDLE_UPRepresents a mouse middle button up event.表示鼠标中键弹起事件。
MIDDLE_CLICK Represents a mouse middle click event.表示鼠标中键点击事件。
MOUSE_MOVE Represents a mouse move event.表示鼠标移动事件。
WHEEL Represents a mouse wheel event.表示鼠标滚轮滚动事件。
PINCH_START  Represents the start of a two-finger event on a touch surface.表示触控屏上双指开始事件。
PINCH_END Represents the end of a two-finger event on a touch surface.表示触控屏上双指结束事件。
PINCH_MOVE  Represents a change of a two-finger event on a touch surface.表示触控屏上双指移动事件。

   
 
    
 
 
 
    
 
   
   
   
   
 
  
 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值