简体   繁体   English

在OpenLayers中选择时覆盖样式属性

[英]Overwrite style properties on select in OpenLayers

I want to reuse my basic style function to create an select style by overwriting some of the properties. 我想重用我的基本样式功能,通过覆盖一些属性来创建选择样式。 This approach is working for fill/stroke colors of linestrings, polygons and texts, but not for images (a circle in my case). 这种方法适用于线串,多边形和文本的填充/描边颜色,但不适用于图像(在我的情况下为圆圈)。 I don't want to create a new image since other properties should be retained. 我不想创建新图像,因为应该保留其他属性。

var selectStyleFunction = function(feature, resolution) {
  var style = styleFunction(feature, resolution)[0];
  style.getImage().getFill().setColor("magenta");

  console.log(style.getImage().getFill().getColor());

  return [style];
};

In this fiddle you can see, that the above code doesn't change the feature style on selection although the log output is correct. 在这个小提琴中,您可以看到,尽管日志输出正确,但是上面的代码并没有更改所选的要素样式。

Is there any way to overwrite this property? 有什么办法可以覆盖此属性?

I found an answer to my question in the api dokumentation of openlayers. 我在openlayers的api文档中找到了我的问题的答案。 It was too obvious to find it in the first place. 首先发现它太明显了。

https://openlayers.org/en/latest/apidoc/ol.style.Style.html https://openlayers.org/en/latest/apidoc/ol.style.Style.html

ol.style.Style ol.style.Style
Container for vector feature rendering styles. 矢量功能渲染样式的容器。 Any changes made to the style or its children through set*() methods will not take effect until the feature or layer that uses the style is re-rendered. 在重新渲染使用样式的要素或图层之前,通过set *()方法对样式或其子项所做的任何更改都不会生效。

So rendering is not triggered by the setters. 因此,渲染不会由设置者触发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM