如何在OpenLayers 4上添加click事件? [英] How do I add a click event on OpenLayers 4?
问题描述
我需要将事件侦听器附加到OpenLayers 4中的功能. 我已经尝试过 feature.on('click',function(){...}),但是它不起作用. 如何为功能添加棕褐色事件? 预先谢谢你.
I need to attach an event listener to a feature in OpenLayers 4. I've tried the feature.on('click',function(){...}) but it is not working. How can i add tan event to a feature? Thank you in advance.
推荐答案
没有为功能ol.Feature
对象注册click
事件.但是ol.Map
存在click
事件.使用forEachFeatureAtPixel
方法获取一个像素的所有特征,并将其与要为其添加侦听器的特征进行比较.
There is no click
event registered for a feature ol.Feature
object. But click
event is present for ol.Map
. Use forEachFeatureAtPixel
method to get all the features at a pixel and compare it with the feature for which you want to add a listener.
相关代码:
var featureListener = function ( event ) {
console.log("featureListenerCalled");
alert("Feature Listener Called");
};
map.on('click', function(event) {
map.forEachFeatureAtPixel(event.pixel, function(feature,layer) {
if ( feature.getId() == "IND" ) {
feature.setStyle(listenerStyle);
featureListener(event);
}
});
});
我已经创建了这个pluckr 链接进行了演示.点击印度地图.
I have created this pluckr link which demonstrates this. Click on India map.
这篇关于如何在OpenLayers 4上添加click事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!