Toggle layers from Mapbox Studio in a webmap?

I just loaded some custom data via a tileset into Mapbox Studio and styled it—there are 5 layers.

I now want to show/hide layers of this data in a webmap and make it interactive/toggleable. Is there a way to display these styled layers such that they can be toggled on/off? I imagine this will just be manipulating the opacity. Or do you need to load in the layers and thenn style them in JS?


1 answer

  • answered 2020-10-01 03:45 Steve Bennett

    You can hide/show a layer in Mapbox GL JS with:

    map.setLayoutProperty('mylayer', 'visibility', 'none'); // or 'visible