简体   繁体   中英

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? 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.

According to the openlayers API if we use click event listeners on map, it will call twice for double click on map. For reference see click event.

So I suggest to use singleclick event, it will not trigger twice when we double click on the map. For reference see singleclick event.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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