jOshT
jOshT

Reputation: 1675

OpenLayers 3 Interaction Style Conflict with ol.feature.setStyle

In OpenLayers 3, if I set a features style with setStyle(...insert style...), the default interactions style with ol.interaction.select no longer appear. If I try to define a style or style function like so:

var select = new ol.interaction.Select({
    style: ...insert style here...
});
map.addInteraction(select);

It will not work. If I remove my custom style set with ol.feature.setStyle(...insert style...), it works fine. Does this make sense? I wonder if setting a features style with setStyle() somehow overwrites the default interaction styles...

Any clues?

Upvotes: 1

Views: 702

Answers (1)

jOshT
jOshT

Reputation: 1675

Instead of using the style or style function when instantiating the ol.interaction.select, I just used the select event to handle the selected and deselected styles.

var select = ol.interaction.select();
select.on('select', function (e) {
    if(e.selected.length > 0) {
        e.selected[0].setStyle(...insert selected style here...);
    }

    if(e.deselected.length > 0) {
        e.deselected[0].setStyle(..insert original style here...);
    }
});

For some reason, when originally defining the style of a feature with setStyle, you cannot use the constructor style or style function when creating the interaction. This above works fine, but it's a little extra code.

Upvotes: 2

Related Questions