简体   繁体   English

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

[英]Mapbox how set fill colour on MGLPolygonFeature (or add feature attributes)

MGLPolygonFeature is supposed to support MGLFeature as a series of attributes on the PolygonFeature. MGLPolygonFeature 应该支持 MGLFeature 作为 PolygonFeature 上的一系列属性。

However, I can find no documentation on how to go about setting attribute features at a polygon level.但是,我找不到有关如何在多边形级别设置属性特征的文档。 Most documentation refers to tile layer, or I am just missing the glue piece I need to solve this issue.大多数文档都涉及瓷砖层,或者我只是缺少解决此问题所需的胶水。

My goal is to assign a fill, opacity, stroke colour, and stroke width to a polygon at the time I create the polygon feature, so that when I create a multitude of polygons, they all have independent fill colours based on some criteria specific to that particular polygon.我的目标是在创建多边形特征时为多边形指定填充、不透明度、描边颜色和描边宽度,这样当我创建多个多边形时,它们都具有基于特定于某些特定标准的独立填充颜色那个特定的多边形。

Some attempted code to solve the issue is provided below - but as can be seen, something is missing in order to set the attributes.下面提供了一些尝试解决问题的代码 - 但可以看出,为了设置属性,缺少一些东西。

    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

The polygon itself does not have layer properties, but the documentation from mapbox seems to suggest that adding attributes is the way to achieve what I want.多边形本身没有图层属性,但 mapbox 的文档似乎表明添加属性是实现我想要的方法。

Any clues to what I am missing?我缺少什么的任何线索?

I used the following methods to solve the addition of colors to polygons.我使用以下方法来解决向多边形添加颜色的问题。

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
}

Called and configured similar to the below.调用和配置类似于以下内容。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM