如何设置填充颜色取决于面要素的属性值?

时间:2018-06-18 20:42:38

标签: ios swift mapbox mglmapview

这是我定义面要素的方法:

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)

但这不起作用。如何设置填充颜色取决于属性?

1 个答案:

答案 0 :(得分:2)

如果要根据属性字典中的颜色值设置多边形的颜色,可以使用an identity function

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

feature selection示例显示了根据值设置填充样式图层样式的其他方法。