I'm looking for an efficient way to set conditionally text for over 3000 features. Below is a little code on how I do it now, but my Android App freezes and crashes sometimes because it exceeds the memory limit of 2 GB. I did try to cache my styles first by color but I end up with only two styles for every feature so I can't set text individually. I did notice that, when I set the text font size to 24px, my app crashes on start, when I return the style function, because of that I set the font size to 18 px.
var layer = new ol.layer.VectorImage({
source: vectorSource,
style: function(feature) {
style.getFill().setColor(getColorByID(feature.get('id'), feature.get('pe_nr')));
return style;
}
});
var style = new ol.style.Style({
fill: new ol.style.Fill({
color: '#FFFFFF'
}),
stroke: new ol.style.Stroke({
color: 'black',
width: 1
}),
text: new ol.style.Text({
font: '18px Calibri,sans-serif',
fill: new ol.style.Fill({
color: 'black'
}),
stroke: new ol.style.Stroke({
color: 'white',
width: 0.5
})
})
});
function switchLabel(label) {
if(label == 'a'){
currentLabel = 'a';
} else if (label == 'b') {
currentLabel = 'b';
} else {
currentLabel = '';
}
var vectorLayer;
var layersObject = map.getLayers();
for (var i = 0; i < layersObject.array_.length; i++) {
vectorLayer = layersObject.array_[i];
break;
}
if(isFoo){
vectorLayer.setStyle(function(feature) {
style.getText().setText(feature.get(currentLabel));
style.getFill().setColor(getColor(feature.get('pe_nr')));
return style;
});
} else {
vectorLayer.setStyle(function(feature) {
style.getText().setText(feature.get(currentLabel));
style.getFill().setColor(getColorByID(feature.get('id'), feature.get('pe_nr')));
return style;
});
}
}
EDIT: It was a bug in Openlayers: https://github.com/openlayers/openlayers/pull/10527