Search code examples
iosswiftmapboxmglmapview

How can I set fill color depends on attribute value of polygon feature?


This is how I define polygon feature:

let polygon = MGLPolygonFeature(coordinates: &coordinates, count: UInt(coordinates.count))
polygon.attributes = ["name": card.name, "identifier": card.identifier, "color": card.set.colorMode] //UIColor

let cardSource = MGLShapeSource(identifier: "cards", features: [polygon], options: [:])

let polygonLayer = MGLFillStyleLayer(identifier: "polygon-level", source: cardSource)
polygonLayer.fillColor = MGLStyleValue(rawValue: "{color}")
polygonLayer.fillOpacity = MGLStyleValue(rawValue: 0.4)

but this doesn't work. How can I set fill color depending on attribute?


Solution

  • If you would like to set the color of a polygon based on a color value in its attributes dictionary, you can use an identity function.

    polygonLayer.fillColor = MGLStyleValue(interpolationMode: .identity, 
                                             sourceStops: nil, 
                                             attributeName: "color",  
                                             options: nil)
    

    The feature selection example shows other ways to style a fill style layer based on a value.