Restricting pointermove interactions to two layers in openlayers 3



In openlayers version v3.6 running in Chrome on Ubuntu

I have create a map with several layers (foo, bar, beltch) in it using the syntax:

layers:[foo,bar,beltch],

I would like to limit interactions to the layers foo and bar The api documents at http://openlayers.org/en/master/apidoc/ol.interaction.Select.html suggest using the following syntax

var selectPointerMove = new ol.interaction.Select({
    condition: ol.events.condition.pointerMove,
    layers:[foo,bar]
 });

But I seem to get events for all layers, I have checked the examples and nothing seem to cover this area unless I have overlooked something.

Does anybody have any suggestions

Answer

Use filter instead of layers. And make sure you set a layer property to compare later.

var layerFeatures = new ol.layer.Vector({
    name: 'selectable',
    source: sourceFeatures
});

var hoverInteraction = new ol.interaction.Select({
    condition: ol.events.condition.pointerMove,
    filter: function(feature, layer){
        if(layer.get('name') === 'selectable')
            return true;
    }
});


Source: stackoverflow