Swift 映射框如何在MGLPolygonFeature上设置填充颜色(或添加要素属性)
MGLPolygonFeature应该支持MGLFeature作为PolygonFeature上的一系列属性 但是,我找不到关于如何在多边形级别设置属性特征的文档。大多数文档都提到了瓷砖层,或者我只是缺少了解决这个问题所需的胶水 我的目标是在创建多边形功能时为多边形指定填充、不透明度、笔划颜色和笔划宽度,以便在创建多个多边形时,它们都具有基于特定于该特定多边形的某些标准的独立填充颜色 下面提供了一些试图解决此问题的代码-但是可以看到,设置属性时缺少了一些内容Swift 映射框如何在MGLPolygonFeature上设置填充颜色(或添加要素属性),swift,mapbox-ios,Swift,Mapbox Ios,MGLPolygonFeature应该支持MGLFeature作为PolygonFeature上的一系列属性 但是,我找不到关于如何在多边形级别设置属性特征的文档。大多数文档都提到了瓷砖层,或者我只是缺少了解决这个问题所需的胶水 我的目标是在创建多边形功能时为多边形指定填充、不透明度、笔划颜色和笔划宽度,以便在创建多个多边形时,它们都具有基于特定于该特定多边形的某些标准的独立填充颜色 下面提供了一些试图解决此问题的代码-但是可以看到,设置属性时缺少了一些内容 let polygon =
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))