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 )
处理用户输入事件。可以添加自定义功能以在以下位置执行当用户输入输出时。
Name | Type | Default | Description |
---|---|---|---|
element | HTMLCanvasElement | document | 可选要向其中添加事件的元素。 |
添加需要绑定的事件的方法,还有其他的方法不一一介绍了,有兴趣的可以参考官方文档
setInputAction (action, type, modifier )
设置要在输入事件上执行的功能。
Name | Type | Description |
---|---|---|
action | function | 输入事件发生时要执行的功能。 |
type | Number | 输入事件的ScreenSpaceEventType。 |
modifier | Number | 可选当 type 时按住的KeyboardEventModifier键事件发生。 |
其中type(
ScreenSpaceEventType)
可用的事件有
名称 | 描述 |
---|---|
LEFT_DOWN | Represents a mouse left button down event.表示鼠标左键按下事件。 |
LEFT_UP | Represents 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_UP | Represents a mouse right button up event.表示鼠标右键弹起事件。 |
RIGHT_CLICK | Represents a mouse right click event.表示鼠标右键单击事件。 |
MIDDLE_DOWN | Represents a mouse middle button down event.表示鼠标中键按下事件。 |
MIDDLE_UP | Represents 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.表示触控屏上双指移动事件。 |