Adrian Garcia
Adrian Garcia

Reputation: 858

Activate / Desactivate layers on leaflet.js

I couldn't find any function to activate / desactivate layers ( overlayMaps ) when you click a button.

For example, I'm trying to make a zoom and activate (display) all the layers when a user clicks a button.

Any idea? Thanks.

Upvotes: 1

Views: 2622

Answers (1)

tmcw
tmcw

Reputation: 11882

Leaflet does not have a concept of active versus inactive layers: you can do one of two things:

  • Make the layer invisible with setOpacity(0) and visible with setOpacity(1)
  • Use removeLayer to remove it, and addTo or addLayer to add it back.

The latter option is better, because it won't load tiles from the inactive layer. This is what the Leaflet layer control does as well behind the scenes.

Upvotes: 2

Related Questions