Remove all labels on Mapbox GL JS?
Asked Answered
M

2

12

I'm using the Mapbox Dark v9 style and would like to remove all the labels.

I found a list of labels here.

And have tried the map.removeLayer function to remove some of them, e.g.:

map.removeLayer("place_label");

As well as:

map.removeLayer("place-city-lg-n");
map.removeLayer("place-city-lg-s");
map.removeLayer("place-city-md-n");
map.removeLayer("place-city-md-s");
map.removeLayer("place-city-sm");

Is there a way to remove labels from a style?

Mannequin answered 8/5, 2017 at 6:31 Comment(0)
L
22

If you're just looking for a dark basemap without labels — i.e. it's not important for you to remove them programmatically at runtime — you can create a new style in Mapbox Studio using the Dark template and use the style editor to select and delete all label layers. You can then publish the style and use its url in your app.

screenshot

If it is important for you to remove all label layers at runtime, you could do something like

map.style.stylesheet.layers.forEach(function(layer) {
    if (layer.type === 'symbol') {
        map.removeLayer(layer.id);
    }
});
Lettie answered 8/5, 2017 at 17:8 Comment(3)
Just a note that that will also remove layers that are symbols, not text "labels". Checking for the presence of a text-field property is probably safer.Milt
Can you say how to get this dark map without any labels in mapboxFictionist
it is not possible to delete main layers now. But we may just hide them.Wording
T
1

Apologies for resurrecting an old question that came up when I Googled it. My goal was to be able to toggle the labels on and off at will, so I modified the above code to this:

let show = false; //or true, etc.                   
map.style.stylesheet.layers.forEach(function(layer) {
    if (layer.type === 'symbol') {
        map.setLayoutProperty(layer.id,"visibility", show?"visible":"none");
    }
});
Thenceforth answered 2/4, 2023 at 16:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.