Hi, Have you the SelectFeatureControl active? That may be the issue.
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: [email protected] [mailto:[email protected]] Im Auftrag von Puneet Kishor Gesendet: Montag, 26. September 2011 20:12 An: [email protected] Cc: [email protected] 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 > _______________________________________________ > Users mailing list > [email protected] > http://lists.osgeo.org/mailman/listinfo/openlayers-users _______________________________________________ Users mailing list [email protected] http://lists.osgeo.org/mailman/listinfo/openlayers-users _______________________________________________ Users mailing list [email protected] http://lists.osgeo.org/mailman/listinfo/openlayers-users
