简体   繁体   English

如何在openlayer中仅为单击注册事件“功能单击”?

[英]How to register an event “feature click” only for single click in openlayer?

map.events.register("featureclick", map, function(e){

}

I want to trigger this event only for single click not for double click. 我只想单击一次而不是双击来触发此事件。

Is it possible in openlayer? 在openlayer中有可能吗? How can I accomplish this? 我该怎么做?

You could try something like the code below, 您可以尝试以下代码,

map.on('click', function(evt){

    var feature = map.forEachFeatureAtPixel(evt.pixel,
        function(feature, layer) {

            //yourCode ;


    });

});

When you click the map the code above will pass each feature its associated layer to the area marked //yourcode. 当您单击地图时,上面的代码会将每个要素的关联层传递到标记为//您的代码的区域。 You can then use an if statement against the layer variable if you only want this to work on features from a certian layer. 然后,如果仅希望此变量对certian图层中的要素起作用,则可以对图层变量使用if语句。

According to the openlayers API if we use click event listeners on map, it will call twice for double click on map. 根据openlayers API,如果我们在地图上使用click事件侦听器,则它将双击两次以调用地图。 For reference see click event. 作为参考, 请参见click事件。

So I suggest to use singleclick event, it will not trigger twice when we double click on the map. 因此,我建议使用singleclick事件,当我们双击地图时,它不会触发两次。 For reference see singleclick event. 作为参考, 请参见singleclick事件。

map.on('singleclick', function(evt) {
    map.forEachFeatureAtPixel(evt.pixel,
        function(feature, layer) {
        //yourCode ;
    });
});

Note that this event is delayed by 250 ms to ensure that it is not a double click. 请注意,此事件会延迟250毫秒,以确保它不是双击事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM