地图框如何在 MGLPolygonFeature 上设置填充颜色(或添加要素属性)



MGLPolygonFeature应该支持MGLFeature作为PolygonFeature上的一系列属性。

但是,我找不到有关如何在面级别设置属性要素的文档。 大多数文档都提到了瓷砖层,或者我只是缺少解决此问题所需的胶片。

我的目标是在创建面要素时为面分配填充、不透明度、描边颜色和描边宽度,以便在创建大量面时,它们都具有基于特定于该特定面的某些条件的独立填充颜色。

下面提供了一些尝试解决此问题的代码 - 但可以看出,为了设置属性,缺少一些东西。

let polygon = MGLPolygonFeature(coordinates: coordinates, count: UInt(coordinates.count))
let identifier = "(name)"
let source = MGLShapeSource(identifier: identifier, shape: polygon)
let fill = MGLFillStyleLayer(identifier: identifier, source: source)
fill.fillColor = NSExpression(forConstantValue: UIColor.green)
fill.fillOpacity = NSExpression(forConstantValue: 0.3)
polygon.attribute(forKey: "fillColor") = fill // non-mutable property cannot be added
return polygon

多边形本身没有图层属性,但 mapbox 中的文档似乎表明添加属性是实现我想要的方法。

我错过了什么的任何线索?

我使用以下方法解决了向多边形添加颜色的问题。

func createSourceAndLayer(identifier: String, shapes: [MGLShape]) -> (MGLSource, MGLStyleLayer) {
let source = MGLShapeSource(identifier: identifier, shapes: shapes)
let layer = MGLLineStyleLayer(identifier: identifier, source: source)
layer.lineColor = NSExpression(forConstantValue: UIColor.white)
layer.lineWidth = NSExpression(forConstantValue: 2)

return (source, layer)
}
func createFillLayer(identifier: String, source: MGLSource) -> MGLStyleLayer {
let fillLayer = MGLFillStyleLayer(identifier: identifier, source: source)
fillLayer.fillColor = NSExpression(forConstantValue: UIColor.red)
fillLayer.fillOpacity = NSExpression(forConstantValue: 0.25)
return fillLayer
}

调用和配置类似于以下内容。

let (source, layer) = createSourceAndLayer(identifier: "sourceId", shapes: polygons)
let fillLayer = createFillLayer(identifier: "fillId", source: source)
style.addSource(source)
// Layer is inserted at position count-1 which works for now but we don't really
// control the z-index of the annotations (cows)
style.insertLayer(layer, at: UInt(style.layers.count - 1))
style.insertLayer(fillLayer, at: UInt(style.layers.count - 2))

最新更新