首页 > 解决方案 > 如何设置填充颜色取决于多边形特征的属性值?

问题描述

这就是我定义多边形特征的方式:

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)

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

标签: iosswiftmapboxmglmapview

解决方案


如果您想根据属性字典中的颜色值设置多边形的颜色,可以使用标识函数

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

特征选择示例显示了基于值设置填充样式图层样式的其他方法。


推荐阅读