英文:
Openlayers styles not applying correctly
问题
// 我试图给一些OpenLayers矢量要素着色,为此我在vectorlayer样式属性中使用了一个函数。该函数使用上面定义的常量样式作为起点,根据要素属性应用要么应用要素存储的颜色,要么应用默认颜色。
我明白当我使用const引用时,我正在编辑每次调用中的同一项,但让我困惑的是它按预期工作。
但当我关闭颜色切换时,它们保留了上一次调用的颜色。为什么?
起点:
首次单击时,执行我想要的操作,但它不应该只应用最后的颜色吗?
再次点击时,我希望它返回到起点,但它却保留了最后的颜色。
英文:
So im trying to color some features in an openlayers vector, for that im using a function in the vectorlayer style property. Said function uses a constant style defined above as a starting point and according to the feature properties it should either apply the color the feature has stored or the default.
I understand that when I use the const reference im editing the same item in every call but what doesn´t make sense to me is that it works as expected.
But then when I toggle the colors off they keep the color from the last call. Why?
Starting point:
When first clicked, does what i want but shouldn't it instead apply only the last color?
When clicked again I wanted it to return to the starting point but instead it keeps the last color.
I have a working stackblitz here: https://stackblitz.com/edit/typescript-wc1d1o?file=index.ts
答案1
得分: 1
以下是翻译好的部分:
-
You don't change the color back to its initial value when
showColor
is false.
当showColor
为假时,您没有将颜色更改回其初始值。 -
You can change the scope of
geomStyle
. In your code it is defined in the global scope. You can move the initialization ofgeomStyle
into thesetFeatureStyle
function.
您可以更改geomStyle
的作用域。在您的代码中,它在全局作用域中定义。您可以将geomStyle
的初始化移动到setFeatureStyle
函数中。 -
The disadvantage on this approach is that multiple instances of
Style
are created.
这种方法的缺点是创建了多个Style
实例。 -
You can also reset the stroke color to its initial value.
您还可以将描边颜色重置为其初始值。 -
The disadvantage on this approach is that you are having side effects which makes the styling unpredictable.
这种方法的缺点是会产生副作用,使样式变得不可预测。 -
You can get the best of both solutions by changing your style function to something like this.
通过将您的样式函数更改为以下方式,您可以兼得这两种解决方案。 -
In this approach you create a new scope where
geomStyle
is defined. It is only initialized once and is then reused on each feature.
在这种方法中,您创建了一个新的作用域,在其中定义了geomStyle
。它只初始化一次,然后在每个要素上重复使用。
英文:
You don't change the color back to its initial value when showColor
is false.
You can change the scope of geomStyle
. In your code it is defined in the global scope. You can move the initialization of geomStyle
into the setFeatureStyle
function.
function setFeatureStyle(feature: FeatureLike): Style {
const geomStyle = new Style({
stroke: new Stroke({
color: '#3399CC',
width: 5,
}),
});
const showColor = feature.get('showcolor');
if (showColor) {
const paramColor = feature.get('color');
geomStyle.getStroke().setColor(paramColor);
}
return geomStyle;
}
The disadvantage on this approach is that multiple instances of Style
are created.
You can also reset the stroke color to its initial value.
function setFeatureStyle(feature: FeatureLike): Style {
const showColor = feature.get('showcolor');
if (showColor) {
const paramColor = feature.get('color');
geomStyle.getStroke().setColor(paramColor);
} else {
geomStyle.getStroke().setColor("#3399CC");
}
return geomStyle;
}
The disadvantage on this approach is that you are having side effects which makes the styling unpredictable.
You can get the best of both solutions by changing your style function to something like this.
function createStyleFunction() {
const geomStyle = new Style({
stroke: new Stroke({
width: 5,
}),
});
const fallbackColor = '#3399CC';
// Return the actual style function
return (feature: FeatureLike) => {
const showColor = feature.get('showcolor');
if (showColor) {
const paramColor = feature.get('color');
geomStyle.getStroke().setColor(paramColor);
} else {
geomStyle.getStroke().setColor(fallbackColor);
}
return geomStyle;
}
}
// Call the `createStyleFunction`
const map = new Map({
layers: [
new VectorLayer({
source: new VectorSource(),
style: createStyleFunction()
})
]
});
In this approach you create a new scope where geomStyle
is defined. It is only initialized ones and is then reused on each feature.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论