opened
ahalota
username_0
* Version of Leaflet (`L.version`):
1.3.1
* Version of esri Leaflet (`L.esri.VERSION`):
 2.1.2

Steps to reproduce the error:

1. Add L.Esri.DynamicMapLayer to map, with 1 subLayer.
`myLayer = L.esri.dynamicMapLayer({ url: layerUrl, layers: [0]}).addTo(map)`
2. Check layer ids on your map
`map._layers >> {32: NewClass, 33: NewClass, 70: NewClass, 85: NewClass, 163: NewClass}`
In my case, 70 is the esriLayer, and 163 is the layer for [0]
3. Remove superLayer
`map.removeLayer(myLayer)`
`map._layers >> {32: NewClass, 33: NewClass, 85: NewClass}`
4. Add the layer again
`map.addLayer(myLayer)`
`map._layers >> {32: NewClass, 33: NewClass, 70: NewClass, 85: NewClass, 172: NewClass}`

You can see that the subLayer [0] has a new idea, 172, instead of the previous 163.
This creates a big issue if attempting to attach any handlers to the subLayer, since it is recreated.
SubLayers of L.Esri.DynamicMapLayer get recreated and assigned new leaflet id on add/remove
issue
created
Biboba
username_1
Hello,

Not sure this is an issue but rather an expected behavior as layer id gets incremented when adding new layers.
Why would you remove layer to add them again ? Could not you set layers as not visible if you want to hide them ?
comment
created
ahalota
username_0
I've always used map.add/removeLayer() to change visibility in leaflet. Is there a different way?

What do you mean by set to not visible? Does this avoid requesting the info from the server (I don't want to request a bunch of rasters and then 'hide' them  with an opacity of 0)
comment
created
Biboba
username_1
For a dynamicMapLayer, you can use setLayers with an empty array:
myLayer.setLayers([])
And it avoids requesting the layer info one more time, contrary to removeLayer and addLayer.
comment
created
ahalota
username_0
While that does work, it doesn't change the unexpected change of layerId. Even if I use setLayers to add/remove sublayers, they are assigned a new layerId. With setLayers, I can understand that a new layerId would be created (since it's hard to predict when switching from [0] to [1,2,] to [0] for example that you are going back to a previously made layer). Underneath everything it probably is just adding/removing the layers as usual.

Based on the way other leaflet layers work, when adding/removing a specific layer it should be added back in the same state it was removed, so the expected behavior for removing then adding a parent layer is that it's child layers retain their original ids as well.
comment
created
jwasilgeo
username_2
If so, can you please create and share a public demo that we can use to investigate and discuss further?
comment
created
ahalota
username_0
This was a while ago, but I'll see if I can take a look later this week...
comment
created
jwasilgeo
username_2
Thanks for your interest and patience! I'd like to dust it off and give it a fresh pair of eyes.
comment