Hi,
Simple question: What is the correct way to remove a layer (ArcGISDynamicMapServiceLayer)?
We do constantly add and remove layers to/from a map object. We noticed that after remove and add there is an exception
Uncaught DOMException: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.
at Object._onLoadHandler (https://js.arcgis.com/3.17/init.js:1857:295)
at HTMLImageElement.<anonymous> (https://js.arcgis.com/3.17/init.js:63:154)
What is wrong? What is the meaning?
Piterson,
It is just map.removeLayer(layer); then layer = null; if you want to completely destroy that layer class.
Robert,
Thank you for your response,
We do not wont to destroy the layer - we will add it next time.
I understand that possible way is to make a layer visible/invisible, but what is wrong to remove/add layer?
Maybe the variable used with map.removeLayer(globalVariable)?
To be specific, it works like:
1)we create new layer
layerGlobalVariable1 = new ArcGISDynamicMapServiceLayer(......)
2)we put to array
dictionaryLayers[layerName1] = layerGlobalVariable1
3)we add to map
globalVariableCurrentLayer = dictionaryLayers[layerName1]
app.map.addLayer(globalVariableCurrentLayer )
4) app.map.removeLayer(globalVariableCurrentLayer)
5) app.map.addLayer(globalVariableCurrentLayer)
the exception is here, BUT not exactly on the row map.addLayer();
Piterson,
I have never had a need to add and remove a layer back and forth like that so I am not sure what is going wrong. I would always remove and recreate the layer personally.
Thanks, Robert,
I see.
I noticed that we create the array as Object:
dictionaryLayers= {};
And after that put layers:
dictionaryLayers[layerName1] = layerGlobalVariable1;
Piterson,
There does not seem to be anything wrong with what you are doing there. But I would re consider your workflow. Why is it necessary to try and hold on to a layer variable after it is removed from the map? This seems to me like a poor use of memory when you can destroy and reconstruct the layer as needed.