3

I am using a Vector Layer with WFS protocol to show only a subset of the data. The filter which defines the subset, changes dynamically from the code. How do I refresh the layer, to show only the data that satisfies the new filter?

layer.refresh(); does not work.

2 Answers 2

3

If your vector layer is called wfslayer, use the the force option on the refresh method like this:

wfslayer.refresh({force: true}); 
1
  • 1
    This also works for an OpenLayers.Layers.XYZ, let's say after an url-change with xyzLayer.setURL(), then just xyzLayer.redraw({force: true});
    – bennos
    Oct 8, 2014 at 11:37
3

you could also do this:

var refresh = new OpenLayers.Strategy.Refresh({force: true, active: true});

and then as a strategy on the WMS layer :

strategies: [new OpenLayers.Strategy.Fixed(), refresh],

then call this where and when needed:

refresh.refresh();

edit:

If you want to test, try this from your init() (onload property content) or in a document.ready block:

  /* Now reload the vector data every 20 seconds */
  setInterval(function () {
        refresh.refresh();
  },20000);

It has to show the async requests in the firebug console and under the tab too, if not your not calling it for some reason.

3
  • I can't get this to work. refresh.refresh() does not cause the layer to refresh. I can't see any request being sent in firebug. Oct 17, 2012 at 3:49
  • Where are you calling this from ? can you show some code in your question? This comes from a production page so it has to work. Update your question with the code you have so far and we'll get this to work.
    – Glenn Plas
    Oct 17, 2012 at 8:16
  • This worked for me. I needed to force the refresh on the Refresh Strategy itself to make my WFS layers trigger the refresh event and go get new data. Thanks.
    – Chris
    Nov 16, 2012 at 22:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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