[OpenLayers-Users] remove a layer by name

Puneet Kishor punk.kish at gmail.com
Mon Sep 26 16:46:50 EDT 2011


On Sep 26, 2011, at 3:02 PM, Puneet Kishor wrote:

> 
> On Sep 26, 2011, at 2:00 PM, Arnd Wippermann wrote:
> 
>> Hi,
>> 
>> Have you the SelectFeatureControl active? That may be the issue. 
> 
> 
> Exactly. That is most likely the issue because, indeed, I do have SelectControl active.


And, confirmed. Deactivating the select control (in fact, I even deactivated the highlight control, to be on the safe side), and then `removeLayer()` works fine.



> 
> Should this tidbit be a part of the documentation? Right where removeLayer is discussed?
> 
> Thanks much.
> 
> 
>> 
>> If you use the Control with an array of layers, it seems, that OpenLayers
>> creates a temporary layer (OpenLayers.Layer.Vector.RootContainer) with all
>> the features of the selectable layers, if the Control is active. 
>> Deleting the layer of the features doesn't delete the features on the
>> temporary layer.
>> Before deleting a layer one have to deactivate the Control.
>> 
>> Arnd
>> 
>> 
>> -----Ursprüngliche Nachricht-----
>> Von: openlayers-users-bounces at lists.osgeo.org
>> [mailto:openlayers-users-bounces at lists.osgeo.org] Im Auftrag von Puneet
>> Kishor
>> Gesendet: Montag, 26. September 2011 20:12
>> An: armin.burger at gmx.net
>> Cc: openlayers-users at lists.osgeo.org
>> Betreff: Re: [OpenLayers-Users] Re: remove a layer by name
>> 
>> 
>> 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
> 



More information about the Users mailing list