Mapbox 10.4 iOS: Changing opacity of raster layer with a slider

277 Views Asked by At

I have just updated from Mapbox 10.3 to Mapbox 10.4, and have an issue with changing the raster layer opacity on the fly. I have a slider to change the raster layer opacity, and had it working in Mapbox 10.3, but as of 10.4 it no longer works.

The code is as follows:

@IBAction func onSliderChanged (_ inSender : Any?)
{
    let opacity = Double(mTransparencySlider.value)
    mRasterLayer?.rasterOpacity = .constant(opacity)
}

I have stepped through the method, and the value is getting passed and stored inside the .rasterOpacity property, but the displayed raster layer does not change opacity.

I have also tried adding a StyleTransition to the raster layer upon creation, but it seems to do nothing.

Is this a Mapbox 10.4 issue, or is there a different way to change opacity of the layer?

1

There are 1 best solutions below

0
On

Eventually found my own answer in the documentation. In the "Work with layers" page, the section titled "Update Layers at Runtime" that contains sample code for adjusting the opacity of a FillLayer.

I extrapolated that to update the opacity of my RasterLayer:

       let opacity = Double(mTransparencySlider.value)
        do {
            try mMapView.mapboxMap.style.updateLayer(withId: mRadarLayerId, type: RasterLayer.self)
            {
                layer in
                layer.rasterOpacity = .constant(opacity)
            }
        }
        catch
        {
            print("Error in trying to change layer opacity")
        }

In Mapbox 6.x, I was keeping track of the RasterLayer and ImageSource in my own code. Mapbox 10.x appears to allow one not to worry about keeping track of the individual sources, instead querying the mapboxMap for style and layer changes using layer/source IDs.