mapbox / mapbox-gl-native

Interactive, thoroughly customizable maps in native Android, iOS, macOS, Node.js, and Qt applications, powered by vector tiles and OpenGL
https://mapbox.com/mobile
Other
4.35k stars 1.33k forks source link

How do I add a layer from another Mapbox Studio style I created? #16539

Closed stonetip closed 3 years ago

stonetip commented 3 years ago

Per the example at https://docs.mapbox.com/ios/maps/examples/runtime-toggle-layer/ I'm able to easily add a layer from one of Mapbox's pre-defined map styles (code included in first func to show exactly what worked for me). However, when I try to add a layer from one of my own styles, it seems to be added but doesn't appear. I'm wondering what I'm missing or if this is even possible. In the same test app, I've used the URL for my second style to create the MapView temporarily so I could check the layer to ensure I'm using the proper sourceIdentifier and sourceLayerIdentifier, e.g.

<MGLLineStyleLayer: 0x282e54080; identifier = layer-friendly-name; sourceIdentifier = mapbox://myco.abc123; sourceLayerIdentifier = layer_src_name; predicate = (null); visible = YES>

func addMapboxExampleLayer(){ // works fine

    let url = "mapbox://mapbox.mapbox-terrain-v2"

    // Add a new vector source and layer.
    guard let srcURL = URL(string: url) else { return }

    guard let mapStyle = mapView.style else { return }

    let source = MGLVectorTileSource(identifier: "contours", configurationURL: NSURL(string: "mapbox://mapbox.mapbox-terrain-v2")! as URL)
    mapStyle.addSource(source)

    let layer = MGLLineStyleLayer(identifier: "contours", source: source)
    layer.sourceLayerIdentifier = "contour"

    layer.lineJoin = NSExpression(forConstantValue: "round")
    layer.lineCap = NSExpression(forConstantValue: "round")
    layer.lineColor = NSExpression(forConstantValue: UIColor.red)
    layer.lineWidth = NSExpression(forConstantValue: 4.0)

    let layerBelowIdentifier = "county-label"

    if let layerBelowInsert = mapStyle.layer(withIdentifier: layerBelowIdentifier) {

        mapStyle.insertLayer(layer, above: layerBelowInsert)
    }
}

This does not work:

func addParcelLayer(){

    let url = "mapbox://styles/stonetip/<my-unique-style-id>"

    // Add a new vector source and layer.
    guard let srcURL = URL(string: url) else { return }

    guard let mapStyle = mapView.style else { return }

    let source = MGLVectorTileSource(identifier: "mapbox://myco.abc123", configurationURL: srcURL)
    mapStyle.addSource(source)

    let layer = MGLLineStyleLayer(identifier: "layer-friendly-name", source: source)
    layer.sourceLayerIdentifier = "layer_src_name"

    layer.lineJoin = NSExpression(forConstantValue: "round")
    layer.lineCap = NSExpression(forConstantValue: "round")
    layer.lineColor = NSExpression(forConstantValue: UIColor.red)
    layer.lineWidth = NSExpression(forConstantValue: 4.0)

    let layerBelowIdentifier = "county-label"

    if let layerBelowInsert = mapStyle.layer(withIdentifier: layerBelowIdentifier) {

        mapStyle.insertLayer(layer, above: layerBelowInsert)
    }
}