I am currently using addFeatures and removeAllFeatures in my code to get the desired results and this is working OK.

However, I have been asked to ensure that the vector layers are not being recreated each time. "They should be loading new data not creating new layers." I'm in no position to argue, and I really think the way I have it set up via removing/adding is pretty decent.

The type of data is airplane data in an AJAX call. The airplane moves along its flight path.

3 layers in question: the airplane layer, the projected flight path, and the travelled flight path.

Currently I have something like so:

if (vectors_airplane != "" || vectors_projected_path != "" || vectors_flown_path != "") {

and then for adding (within the same ajax call):


These are vector layers but there is no associated WFS. It seems to use the refresh strategy appropriately there must be a WFS Protocol (please correct me if I am wrong). I don't want to add a WFS protocol to each layer here, as it seems unnecessary.

The goal: how to change this logic to not use removeAllFeatures and addFeatures and instead use refresh/redraw?

  • I'm having trouble understanding your issue. Is it currently working or not? Where did the WFS come in? A Vector layer can be of many types, and WFS is just one of them. And finally, what benefit would you derive by using refresh/redraw instead of what you are currently doing? Commented Dec 4, 2013 at 3:10

1 Answer 1


Here is what really happens.

A Vector Layer is basically just a container for Graphics in the map. It can come from WFS, a GeoJSON file, GeoRSS, KML, an Ajax Request (like in your case), from a drawing control of the map, and many more.

A Vector Layer has an array of features. If a protocol has been set for the layer (i.e. You have told the layer where to get the data from), OpenLayers will use that to request for data, and have those features in the features array. How much data is requested, and when, is decided by the Strategy that is set on the Vector Layer.

Just using removeAllFeatures and addFeatures on the Layer will not 'add and remove the layer'. It is the same layer. the layer is not destroyed and recreated.

To prove this, you can see that the layer id remains the same. You could also listen to the layer's removed & added event, and see when they are fired. If you want even more proof, just read the source code.

If you still need to use the refresh method, this is what I would do.

Initialize the Vector layers, with the HTTP Protocol. Make sure it points to your service which returns data for each layer (currently you might be having one service for all three layers. You will have to break it down into 3 different services, or at least pass different data depending on a parameter). Use the Fixed Strategy. Set the Appropriate Format.

Here is an example which does something similar, with gpx data: Vector Behavior Example

And when you need to refresh the data, just call the following:

vectors_flown_path.refresh({force: true}); 
vectors_projected_path.refresh({force: true}); 
vectors_airplane.refresh({force: true});

You will see that a request will be made to the service, and new data will be returned.

Behind the scenes, this and your present procedure are almost same, so it's up to you whether you follow this procedure or your current procedure. Performance wise, there will be no difference.

  • Never thanked you for the very detailed answer, much appreciated. Seems like the issue is a memory leak or something along those lines, as the interactivity (ie clicking on popups, panning around, screen at times going white/blank) becomes strange after a while: like 3-5minutes. Between commenting out the AJAX calls and observing the memory usage in Chrome, not sure what is going on.
    – Nubtacular
    Commented Dec 9, 2013 at 17:14
  • 1
    Without looking at your complete code it's quite difficult to say where the memory leak is happening. I would try to find out the cause of the leak by using Chrome's memory Profiler Commented Dec 10, 2013 at 2:46
  • Yep, have been using that. Definitely helpful and after some js refactoring, I'm not having any performance issues, but still using quite a bit of memory (taps out at like 120k, which seems high).
    – Nubtacular
    Commented Dec 12, 2013 at 18:49
  • That's not necessarily high. It all. Depend on the number of markers that you have . I've seen much higher consumption. Commented Dec 13, 2013 at 1:05

Your Answer

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

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