OpenLayers 样式未正确应用

huangapple go评论70阅读模式
英文:

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:

OpenLayers 样式未正确应用

When first clicked, does what i want but shouldn't it instead apply only the last color?
OpenLayers 样式未正确应用

When clicked again I wanted it to return to the starting point but instead it keeps the last color.
OpenLayers 样式未正确应用

I have a working stackblitz here: https://stackblitz.com/edit/typescript-wc1d1o?file=index.ts

答案1

得分: 1

以下是翻译好的部分:

  1. You don't change the color back to its initial value when showColor is false.
    showColor 为假时,您没有将颜色更改回其初始值。

  2. 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.
    您可以更改 geomStyle 的作用域。在您的代码中,它在全局作用域中定义。您可以将 geomStyle 的初始化移动到 setFeatureStyle 函数中。

  3. The disadvantage on this approach is that multiple instances of Style are created.
    这种方法的缺点是创建了多个 Style 实例。

  4. You can also reset the stroke color to its initial value.
    您还可以将描边颜色重置为其初始值。

  5. The disadvantage on this approach is that you are having side effects which makes the styling unpredictable.
    这种方法的缺点是会产生副作用,使样式变得不可预测。

  6. You can get the best of both solutions by changing your style function to something like this.
    通过将您的样式函数更改为以下方式,您可以兼得这两种解决方案。

  7. 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.

huangapple
  • 本文由 发表于 2023年3月3日 20:53:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75627332.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定