简体   繁体   中英

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.

Now I found a working solution:

You can add an additional argument (eg 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);
                }
            })

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/

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