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

佐手、 提交于 2020-01-15 20:26:05

问题


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.


回答1:


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);
                }
            })



回答2:


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/



来源:https://stackoverflow.com/questions/35184546/openlayers3-same-style-for-selected-features-only-one-changed-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!