0

When I use the following code to go through all the layers in a leaflet map,

map.eachLayer(function (layer) {
   console.log(layer);
});

I just wants what rule is used to create this order. Is it because of the ZIndex of the layer? Or is it because the layer is added in different time? If the layer is added early, it will be in the bottom.

Thanks,

Jerry Yuan
  • 503
  • 2
  • 5
  • 15

1 Answers1

1

Good question, it seems the LeafletJS docs don't say anything about the ordering of the layers via eachLayer, so...

Looking at the code on github, L.Map.eachLayer loops through L.Map._layers which is an object (not an array). So technically the layers could come back in any order...

That said, the individual layers are added to the _layers object in the order they are added to the map. So in all likelihood, eachLayer will be iterating over the layers in that same order (whatever order they were added to the map).

Community
  • 1
  • 1
sfletche
  • 36,606
  • 25
  • 86
  • 108