【问题标题】:Mapbox how set fill colour on MGLPolygonFeature (or add feature attributes)Mapbox 如何在 MGLPolygonFeature 上设置填充颜色(或添加要素属性)
【发布时间】:2020-08-09 17:22:53
【问题描述】:

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

我缺少什么的任何线索?

【问题讨论】:

    标签: swift mapbox-ios


    【解决方案1】:

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

    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))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-05
      • 2012-07-16
      • 2018-06-09
      • 2020-01-31
      • 1970-01-01
      • 2015-02-22
      • 1970-01-01
      • 2019-01-16
      相关资源
      最近更新 更多