I'm trying to add a style function programmatically to a VectorLayer
in my map, but nothing is displayed and no errors are fired. Before, I used to add all the layers and styles manually, but now I want to do this dynamically. Inside the style function I used the switch
method to change the color for each feature value, this way:
var randomVectorLayer = new VectorLayer({
...
style: (feature) => {
let featureValue = feature.get("SOME_ATTRIBUTE");
switch(featureValue) {
case "VALUE1":
return this.styleArea('rgba(100, 255, 0, 1)'); // returns a style for Areas with the passed color
break;
case "VALUE2":
return this.styleArea('rgba(85, 255, 0, 1)', 'rgba(0, 0, 0, 1)', 2);
break;
}
},
...
});
Now I trying to add programmatically, which I already made it for simple polygons with a single style, this way:
let styleNewLayer;
if (attr_array.length == 1) { // simple layer with only one attribute
styleNewLayer = this.styleArea(attr_array[0]['color']);
}
let newVectorLayer = new VectorLayer({
...,
style: styleNewLayer,
...
});
But polygons that contain many variations of attributes and consequently several styles proved to be more challenging for me. One of the ways I tried to load the styles was this way, changing the switch
function for a if
one:
let styleNewLayer;
if (attr_array.length > 1) {
styleNewLayer = (feature) => {
attr_array.forEach(attr => {
let featureValue = feature.get(attr.name); // I can see that this value is correctly acquired
if (featureValue == attr.value) { // I tried to set this value to a known one as well.
return this.styleArea(attr.color);
}
});
};
}
let newVectorLayer = new VectorLayer({
...,
style: styleNewLayer,
...
});