openlayers3 所选功能的相同样式(只有一个更改 属性)?

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

我有一个带有样式定义的 ol3 图层。我想对 select 交互使用相同的样式:

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

我只想更改一个 属性 (iconSize) 样式以突出显示 selected 功能。这有可能吗?我不想定义两个单独的样式,但如果可以通过编程方式复制样式并替换图标大小值,我会很好。

可能的解决方案:告诉您的函数 ol.interaction.Select 处于活动状态:

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/

现在我找到了可行的解决方案:

您可以向样式函数添加一个额外的参数(例如突出显示 [bool]):

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

而不是

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

你可以使用

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