[OpenLayers-Users] Re: remove a layer by name

Armin Burger armin.burger at gmx.net
Mon Sep 26 13:57:19 EDT 2011



On 26/09/2011 05:31, Puneet Kishor wrote:

>
> The following code does what I want --
>
> 	var lyr_list = map.getLayersByName("a_layer");
>          if (typeOf(lyr_list) === "array") {
>          	for (var i in lyr_list) {
>                  	lyr_list[i].removeAllFeatures();
>                          removeLayer(lyr_list[i]);
>                  }
> 	}
>
 > Surely, the above couldn't be the most optimum way, could it?


it will not change anything for your particular case, but I usually 
reference layers via their ID. This requires to add the ID after creation.

something like

var myLayer = new OpenLayers.Layer...;
myLayer.addOptions({id:'theLayerId'});

then you can always access the layer later on like

var mapLayer = map.getLayer('theLayerId');


For completely deleting a layer it might be necessary to also run the 
.destroy() method on the layer, but I'm not sure when this is required 
and when not.

Regards
Armin


More information about the Users mailing list