0

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 的文档似乎表明添加属性是实现我想要的方式。

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

4

1 回答 1

0

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

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))
于 2020-08-09T09:22:53.430 回答