Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change visibility of layer in custom style with Mapbox JS GL?

Tags:

mapbox

I am a beginner in mapbox JS GL. I am looking for a way to give the user the opportunity to change visibility of layer in mapbox on button click.
In MapBox studio I add to Basic style visible layer "regions". I tried to do so :

<script> ...
var mapp = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/terentev/ck2so0c4h1q5x1cqow0aj9nh8',
center: [34.047, 63.779],
zoom: 5.41
});
mapp.setLayoutProperty('regions','visibility','none');

But the layer does not disappear.

And when I try get layers from style:

var v = mapp.getStyle().layers;

I can't. How to do it right?
Thanks in advance!


This is layer 'regions', added to Basic style : layer 'regions' in mapbox studion I tried like this:

var v = mapp.getLayoutProperty('regions', 'visibility');
 alert('visibility '+ v );
 mapp.setLayoutProperty('country-label','visibility','visible');
 v = mapp.getLayoutProperty('regions', 'visibility');
 alert('visibility '+ v );

On first alert I get "visibility undefined" but there is no result on second alert at all

like image 439
А.Терентьев Avatar asked Dec 04 '25 18:12

А.Терентьев


1 Answers

Try this:

var mapp = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/terentev/ck2so0c4h1q5x1cqow0aj9nh8',
center: [34.047, 63.779],
zoom: 5.41
});

mapp.on('load', () => {
     mapp.setLayoutProperty('regions','visibility','none');
})

It just looks like you're invoking setLayoutProperty before the map has properly loaded

like image 94
Lucas Dolsan Avatar answered Dec 08 '25 21:12

Lucas Dolsan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!