[OpenLayers-Users] Re: remove a layer by name
Puneet Kishor
punk.kish at gmail.com
Mon Sep 26 14:11:36 EDT 2011
On Sep 26, 2011, at 12:57 PM, Armin Burger wrote:
>
>
> 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');
Thanks Armin, but you are correct, it doesn't change anything for me. I have no problems "getting" the layer. I just am unable to use that layer to remove it effectively (and, I'd prefer to continue getting the layer via its name).
Some of the things I don't understand --
Why is a layer list returned and not just a layer when I `getLayersByName`? In other words, under what use case would there be more than one layers with the same name?
Why is a layer removed from the layer switcher via `removeLayer(layerObj)` but its features not removed? What could be the use of that functionality?
Why are the features of a layer removed from the map via `removeAllFeatures()` but the layer not removed? (although I can see the point of that).
Having a clear distinction between removing the features, removing the layer, and destroying the layer would also be very useful.
>
>
> 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
> _______________________________________________
> Users mailing list
> Users at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/openlayers-users
More information about the Users
mailing list