Chris
Chris

Reputation: 14260

openlayers3 same style for selected features (only one changed property)?

I have an ol3 layer with a style definition. I would like to use the same style for the select interaction:

style = function(feature, resolution) {

    var iconFont = 'FontAwesome';
    var iconFontText = '\uf1f8'; // fa-trash
    var iconSize = 24;
    var col = 'black';

    var styles = [];

    var styleIcon = new ol.style.Style({
        text: new ol.style.Text({
            font: 'Normal ' + iconSize + 'px ' + iconFont,
            text: iconFontText,
            fill: new ol.style.Fill({color: col})
        })
    });
    styles.push(styleIcon);

    }

    return styles;
};

    new ol.layer.Vector({
            source: that.source,
            style: style
        });

    new ol.interaction.Select({
                features: that.selectedFeatures,
                style: style
            })

I only want to change one property (iconSize) of the style to highlight the selected features. Is that somehow possible? I do not want to define two seperate styles, but I would be ok if it was somwhow possible to programmatically copy the style and replace the iconsize value.

Upvotes: 1

Views: 1396

Answers (2)

Jonatas Walker
Jonatas Walker

Reputation: 14168

A possible solution: Tell your function that ol.interaction.Select is active:

var style = function(ft, res, selecting){
  return [
    new ol.style.Style({
      stroke: new ol.style.Stroke({
        color: '#ffcc33',
        width: selecting ? 4 : 2
      })
    })
  ];
};

var selecting = false;
selectInteraction.on('select', function(evt) {
    selecting = evt.selected.length > 0;
});

http://jsfiddle.net/jonataswalker/eepyne75/

Upvotes: 0

Chris
Chris

Reputation: 14260

Now I found a working solution:

You can add an additional argument (e.g. highlight [bool]) to the style function:

style = function(feature, resolution, highlight) {
...
}

and the instead of

new ol.interaction.Select({
                features: that.selectedFeatures,
                style: style
            })

you can use

new ol.interaction.Select({
                features: that.selectedFeatures,
                style: function(feature,resolution){
                     return style(feature,resolution,true);
                }
            })

Upvotes: 2

Related Questions