[OpenLayers-Users] Re: remove a layer by name
Puneet Kishor
punk.kish at gmail.com
Sun Sep 25 23:27:04 EDT 2011
Hmmmm... the mystery deepens. Turns out, when I do the following
var lyrs = map.getLayersByName("a_layer");
map.removeLayer(lyres[0]);
The layer does get removed from the layer switcher, but its features remain drawn on the map. That's not good because subsequent drawing of that layer results in twice as many features on the map.
On the other hand, if I do the following
var lyrs = map.getLayersByName("a_layer");
lyrs[0].removeAllFeatures();
Then the features get removed from the layer but the layer itself remains in the layer switcher. That's not good because while my map now looks fine, the layer switcher now has two layers (and more if I rinse, lather, repeat) called "a_layer".
I am not sure what the right terminology is, but I effectively want to remove the layer so its features disappear. Then I can redraw that layer.
Suggestions?
On Sep 25, 2011, at 9:42 PM, Puneet Kishor wrote:
> 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