绑定事件:map.on(type, listener)
取消绑定:map.un(type, listener)
type:事件类型
listener:执行得函数体
事件类型:
//事件类型
let type = {
click:'click',//单击
dblclick:'dblclick',//双击,双击会触发click
singleclick:'singleclick',//单击,延迟250毫秒,就算双击不会触发
moveend:'moveend',//鼠标滚动事件
pointermove:'pointermove',//鼠标移动事件
pointerdrag:'pointerdrag',//鼠标拖动事件
precompose:'precompose',//地图准备渲染,为渲染
postcompose:'postcompose',//地图渲染中
postrender:'postrender',//地图渲染全部结束
changeLayerGroup:'change:layerGroup',//地图图层增删时触发
changeSize:'change:size',//地图窗口发生变化就会触发
changeTarget:'change:target',//地图绑定的div发生更改时触发
changeView:'change:view',//地图view对象发生变化触发
propertychange:'propertychange',//Map对象中任意的property值改变时触发
}
例子:
//绑定事件
let fn = function(e){
console.log(e)
}
map.on(type.click,fn)
//取消绑定
setTimeout(()=>{
map.un(type.click,fn)
},3000)
通常结合使用的方法:
map.forEachFeatureAtPixel(pixel, callback)
map.on('click',function(e){
//屏幕坐标
let pixel = this.map.getEventPixel(e.originalEvent);
//检测与视口上的像素相交的要素
map.forEachFeatureAtPixel(pixel,function(feature,layers){
//feature,返回的要素
console.log(feature)
//layers,返回的图层
console.log(layers)
})
})
原文出处:https://www.cnblogs.com/MaShuai666/p/12494296.html