3

I want to set the style on run time, but I don't want the layer to auto refresh.

I want to be able to perform some changes before I manually refresh the layer.

I looked into the setStyle method and I wrote my own customSetStyle method:

// Exactly like the 'setStyle' method from OL3
customSetStyle = function(style) {
    this.style_ = style !== undefined ? style : ol.style.defaultStyleFunction;
    this.styleFunction_ = style === null ?
      undefined : ol.style.createStyleFunction(this.style_);
    //this.changed();    // I commented this line out
};

By commenting out the this.changed(); line, the layer is not auto refreshed anymore.

I want to know if there is a better way to prevent the layer from auto refreshing AND if the way I found has nasty consequences.

1

You are using private, non-API properties here (like styleFunction_, style_). You should never be doing that. Instead, look at the API documentation to find out about available methods and properties.

If you want to build a style without applying it, create an ol.style.Style, but do not call setStyle() on the layer unless you have the final style.

Also note that the layer will not be immediately redrawn when you call setStyle(), and also not every time you call setStyle(). The map renderer requests an animation frame, and the layer will be refreshed with all changes at once when the browser is ready to render.

  • Thank you for your answer, @ahocevar. For me, it seems that calling setStyle refreshes the layer immediately. But you said "the layer will be refreshed with all changes at once when the browser is ready to render". How can I do that? – joaorodr84 Aug 1 '16 at 15:25
  • No need for you to do anything here. This is an optimization that OpenLayers makes with the help of the browser. The update feels immediate, but all style changes you make within 16 milliseconds (assuming a typical frame rate) will cause the map to be refreshed only once. – ahocevar Aug 2 '16 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.