vue-leaflet/Vue2Leaflet

LControlLayers or layer visibility status

josejachuf opened this issue · 1 comments

How can I capture the state of the component (Layer / visibility) to be able to save it? Or do I iterate over the map to see the visibility status of the layer?

I did it with:

this.$refs.map.mapObject.eachLayer(layer => {
    console.log(layer)
})

but I can't access either name or visible

What I want is when I return to the map to be able to reproduce the latest layer / visibility status

Versions

  • Leaflet: v1.7.1
  • Vue: v2.9.6
  • Vue2Leaflet: v2.6.0
mikeu commented

Hi @josejachuf , as far as I can see from the L.Control.Layers documentation, Leaflet does not provide any methods for querying the current state.

It looks to me as if the eachLayer function is only returning the layers that are visible (at least, for base layers), so it might be safe to assume that if a layer is not in that list then it is not visible. However I agree with you, it doesn't appear as if Leaflet provides a documented method for uniquely identifying the layer within the callback.

You should be able to get around this by setting a custom property on the layer, such as <l-tile-layer :options="{ myLayerName: layer.name }"> and looking at layer.options.myLayerName in your callback.

For the most part though, I think that whatever the best answer to your question is going to be, it will lie with Leaflet itself, as you are really asking about its core functionality here, not anything that Vue2Leaflet does on top of that.