1

Does anybody know of an equivalent to the Leaflet L.layerGroup in Mapbox GL JS? I can do this in leaflet:

var layerGroup = L.layerGroup().addTo(map);

And then if I want to add, say, a marker to the layer group, I can do

L.marker([50.5, 30.5]).addTo(layerGroup);

The functionality I am really trying to get is the ability to remove and add back these layers. In leaflet you can do

// remove the layer from the map
map.removeLayer(layerGroup)

// re-add the layer to the map
layerGroup.addTo(map)

// clear all layers without removing from the map
layerGroup.clearLayers()

However in Mapbox GL JS I haven't found a functionality or a plugin that does something similar to this. When you want to add something to the map in Mapbox, you can do .addTo(map), so it seems to me there is probably a feature where you can create a layer group and do .addTo(layerGroup).

It might not be exactly how I envision it here, but right now I have nothing.

0

1 Answer 1

0

Concept of layer groups was present in predecessor Mapbox JS (see https://docs.mapbox.com/mapbox.js/api/v3.3.1/l-layergroup), but was left out in it's successor Mapbox GL JS.

However there's a simple Mapbox plugin for layer groups available at https://github.com/mapbox/mapbox-gl-layer-groups. There are no docs, you'll have to look in the code how to use it.

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.