[GIS] getActiveLayers for LeafletJS

leaflet

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…

Best Answer

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

Related Question