How to find all layers in Mapboxgl ? Ultimately I want to show custom layer only on water and not on land
Asked Answered
S

3

30

I created a custom circle layer. I want to show this layer only on water and not on land. I managed to do the opposite (ie: showing the layer on land and not on water) using below command. Refer this image for better understanding

map.moveLayer('polygon','water');

Now I need to know the land layer which is used by mapboxgl so that I can call function map.moveLayer('polygon','land'); to achieve what i want.

I need help to find the different layers present in the mapboxgl-streets map. But unfortunately, Mapboxgl doesn't have map.eachLayer function.

Skin answered 23/3, 2017 at 14:27 Comment(0)
P
76

You can use the Map#getStyle method to get a serialized representation of the entire style including the layers.

map.getStyle().layers
Prorate answered 23/3, 2017 at 18:33 Comment(0)
G
3

Just to add to Lucas' answer (which is still correct), map.getStyle().layers provides all layers in the style, including ones you have explicitly added (via map.addLayer()), and those that are included in the style (which could be a lot). Careful how you filter through these. For my case, I created arrays to hold the layers I created myself, to make future iteration simpler.

Grouse answered 12/3, 2021 at 16:31 Comment(0)
S
1

It depends on the map style you're using. In general, you either have to look at its source or load it in Mapbox Studio to identify the correct layer name. Also keep an eye on https://github.com/mapbox/mapbox-gl-js/issues/4173.

Sustentacular answered 23/3, 2017 at 15:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.