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

Puneet Kishor punk.kish at gmail.com
Sun Sep 25 22:42:58 EDT 2011


Am at my wit's end trying to remove a layer. Would welcome any suggestion on the following. Essentially, I want to remove a layer called "foo" from my map, so I may redraw "foo" with new data.


On Sep 25, 2011, at 9:04 AM, Mr. Puneet Kishor wrote:

> I spoke too soon...
> 
> On Sep 24, 2011, at 3:58 PM, Mr. Puneet Kishor wrote:
> 
>> I would like to remove a layer by name. The only method I see for removing layer seems to take a layer object, which implies something like so
>> 
>>   var lyr_list = map.getLayersByName(name);
>> 
>> Why does the above return a list? Under what use case would one have more than one layer by the same name? In any case, I have all layers with unique names, so the list would always have only one element.
>> 
>>   map.removeLayer(lyr_list[0]);
>> 
>> The above works, but is there a more efficient or better way?
> 
> 
> Here is my code that does not work
> 
>    var lyrs = map.getLayersByName("a_layer");
>    alert("got " + typeof(lyrs[0]) + 
>        " called " + lyrs[0].name + 
>        " with id " + lyrs[0].id);
>    map.removeLayer(lyrs[0]);
> 
> The alert window shows the correctly that it got an object with the right layer name and id. Yet, the layer is not removed from the map... at least, it is still visible, so when the new layer draws, I see a mess of features.
> 
> What is my error?
> 
> Thanks,
> 
> Puneet.



More information about the Users mailing list