5

I'd like to identify the layers that are currently active (which basemap is currently in view and which overlays are currently turned on).

Was hoping to find something akin to getActiveLayers in the Leaflet API (http://leafletjs.com/reference.html/) but I'm not seeing anything...

  • yeah you gotta manage this yourself as far as i know. or use the plugin. I'm guessing the plugin subscribes to baselayerchange event and keeps track of the layer for you, which is how you do it without the plugin – nothingisnecessary Jul 29 '16 at 2:02
6

Well, one option I've come across (to answer my own question) is a plugin created by vogdb.

This plugin contains methods getActiveBaseLayer() and getActiveOverlayLayers().

The following, from vogdb's github page, demonstrates how vogdb's activeLayers control can be used in place of the standard leaflet layers control making the 2 methods available.

var control = L.control.activeLayers(baseLayers, overlayLayers)
control.addTo(map)

console.log(control.getActiveBaseLayer().name)

var overlayLayers = control.getActiveOverlayLayers()
for (var overlayId in overlayLayers) {
    console.log(overlayLayers[overlayId].name)
}

The plugin can be found on github: https://github.com/vogdb/Leaflet.ActiveLayers

And for referencing purposes, I found the link to vogdb's plugin here: http://leaflet.uservoice.com/forums/150880-ideas-and-suggestions-for-leaflet/suggestions/3777550-get-active-baselayer

  • 1
    seems like a very basic need, I don't understand why leaflet team didn't put it on the core – Rodrigo E. Principe May 11 '18 at 14:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.