Javascript openlayers3选定功能的相同样式(仅一个更改的属性)?
我有一个带有样式定义的ol3图层。我希望对选择交互使用相同的样式:Javascript openlayers3选定功能的相同样式(仅一个更改的属性)?,javascript,openlayers-3,Javascript,Openlayers 3,我有一个带有样式定义的ol3图层。我希望对选择交互使用相同的样式: 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 = 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)以高亮显示选定的特征。这有可能吗?我不想定义两种不同的样式,但如果可以通过编程方式复制样式并替换iconsize值,我会同意的。一个可能的解决方案:告诉您的函数
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;
});
现在我找到了一个有效的解决方案: 您可以向style函数添加一个附加参数(例如高亮显示[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);
}
})
我认为这也会影响未选择的功能,因为它们共享相同的样式?你测试代码了吗?当取消选择时,功能会恢复到原始样式。是的,我选择了。当一个功能图标被选中时,所有功能图标的样式都会改变。它只在功能重新启动之前有效。选择要素后尝试移动地图。这将触发刷新,所有功能都将适应新的样式规则。这是一个很好的解决方案。我已经更新了我的小提琴,包括你的解决方案。