我正在尝试以编程方式将样式功能添加到地图中的VectorLayer
上,但是没有显示任何内容,也没有引发任何错误。以前,我曾经手动添加所有图层和样式,但是现在我想动态地添加。在样式函数内部,我使用了switch
方法来更改每个特征值的颜色,方法是:
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;
}
},
...
});
现在,我尝试以编程方式添加,这种方式已经可以通过一种方式将其用于具有单一样式的简单多边形:
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,
...
});
但是包含许多属性变化并因此有多种样式的多边形对我来说更具挑战性。我尝试加载样式的方法之一就是这种方式,将switch
函数更改为if
:
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,
...
});