openlayers點擊事件

1. 事件監(jiān)聽 -- feature

// 為地圖注冊鼠標(biāo)單擊事件的監(jiān)聽

map.on('singleclick', event => {

????map.forEachFeatureAtPixel(event.pixel, feature => {

????// 為移動到的feature發(fā)送自定義的singleclick消息

? ? ? ? feature.dispatchEvent({type:'singleclick', event: event});

? ? });

});

// 為某個feature注冊事件監(jiān)聽

feature1.on('singleclick', event => {

? ? console.log(event);

})

上面的事件類型換成dbclick,mousemove等同理

2. select -- layer

// 創(chuàng)建圖層的動作

let selectClick =new ol.interaction.Select({

? ? condition :ol.events.condition.singleClick,

? ? style : areaStyles,

? ? filter: (feature, layer) => {

????????return layer === myLayer // 需要添加事件的layer

????}

});

// 地圖實例中加入事件

selectClick.on('select', event => {

? ? console.log(event);

? ??if (event.selected[0] == null ) return;

? ? //?在使用ol.interaction.Select進行點擊時,默認(rèn)會把點擊選中的要素顯示在地圖上

? ??// 可以用以下方法解決

? ? selectClick.getFeatures().clear();??

});

map.addInteraction(selectClick);

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容