2

This is an example of how opacity can be changed for a Mapbox.JS map: https://www.mapbox.com/mapbox.js/example/v1.0.0/opacity/

The example shows only one layer used.

With barely any experience in JavaScript, h*ow would I go about changing the opacity of the active base layer, if I have multiple of them?*

I have this now:

 L.control.layers({
    'Name of layer': L.tileLayer('/tiles1/z{z}/{y}/{x}.png').addTo(map),
    'Name of other layer': L.tileLayer('/tiles2/z{z}/{y}/{x}.png')
    }).addTo(map);

How to correctly set the variable to the selected layer?

1 Answer 1

2

One solution would be to set the layers object to its own variable:

var myLayers = {
    'Name of layer': L.tileLayer('/tiles1/z{z}/{y}/{x}.png').addTo(map),
    'Name of other layer': L.tileLayer('/tiles2/z{z}/{y}/{x}.png')
};
L.control.layers(myLayers).addTo(map);

You could then set the opacity to either layer at any time with something like the following:

myLayers['Name of layer'].setOpacity(.8);
myLayers['Name of other layer'].setOpacity(.3);
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.