On Sat, Feb 5, 2011 at 11:05 PM, Tom Chance <[email protected]> wrote:
> Hello,
> I'm trying to write some code to remove all the overlays (so I can add a
> fresh bunch). I've got the following function:
> function kill_overlays() {
>   for (lyr_name in map.layers) {
>     lyr = map.layers[lyr_name];
>     if (!lyr.isBaseLayer) {
>       map.removeLayer(lyr);
>     }
>   }
> }
> But strangely, it doesn't remove all the layers. If you call it several
> times over it eventually removed the lot. Can anyone see the problem? You
> can play with a live test here:
> http://www.openecomaps.co.uk/map_test.html

You must be cautious when iterating and removing elements. One
technique involves iterating in the reverse order:

for(var i=map.layers.length-1; i>=0; i--) {
    var l = map.layers[i];
    if(!l.isBaseLayer) {
        map.removeLayer(l);
    }
}




-- 
Eric Lemoine

Camptocamp France SAS
Savoie Technolac, BP 352
73377 Le Bourget du Lac, Cedex

Tel : 00 33 4 79 44 44 96
Mail : [email protected]
http://www.camptocamp.com
_______________________________________________
Users mailing list
[email protected]
http://lists.osgeo.org/mailman/listinfo/openlayers-users

Reply via email to