[Mapbox] How to change the layer order?

Hi,

Does anyone know how to change the layer order on a Mapbox map?

I have a bunch of layers and they are all stacked on top of the base style labels (on top of country/city labels, etc.)

Is there a way to use addLayer(beforeId) ?

Thanks !

Hi William :waving_hand:

I’ve noticed some other members commenting about using layers in Mapbox as being something tricky.

I’m going to ask our team if they know about a potential issue there.

1 Like

Hi @WilliamB can you see this works for you:

Add custom JS action on map load that uses the Mapbox instance variable:

const map = /* Mapbox element's .instance variable */;

const labelId = map.getStyle().layers.find(l => l.type === 'symbol')?.id;

map.moveLayer('my-layer-id', labelId);

map.addLayer({

id: 'my-layer-id',

type: 'fill',

source: 'my-source',

paint: { 'fill-opacity': 0.5 }

}, labelId);

Hello Agustin,

Sorry for not replying sooner.

The problem with this method is that my users can filter features from a vector tileset source that is tied to many layers on my mapbox instance and I’m not sure if this will work well.