Mapbox ios 按颜色特征属性设置MGLFillStyleLayer的FillColor
我们正在开发一个iOS应用程序,但在设置MGLFillStyleLayer(MapBox iOS SDK)的填充颜色时遇到问题 我们有一个大的.geojson文件,它被解析并添加到标准MapBox映射中。geojson中的每个功能都有一个“color”属性来设置功能的背景色。颜色保存为十六进制代码 是否可以使用MapBox表达式或类似“forEach feature->set fill color”的内容分别设置每个要素的颜色 我们试图通过使用MapBox提供的用于样式设置()的表达式来更改颜色,但不知道如何将feature属性加载到swift函数中以生成颜色。 在Mapbox()的热图示例中,我们看到可以通过NSNumber值设置填充颜色Mapbox ios 按颜色特征属性设置MGLFillStyleLayer的FillColor,mapbox-ios,Mapbox Ios,我们正在开发一个iOS应用程序,但在设置MGLFillStyleLayer(MapBox iOS SDK)的填充颜色时遇到问题 我们有一个大的.geojson文件,它被解析并添加到标准MapBox映射中。geojson中的每个功能都有一个“color”属性来设置功能的背景色。颜色保存为十六进制代码 是否可以使用MapBox表达式或类似“forEach feature->set fill color”的内容分别设置每个要素的颜色 我们试图通过使用MapBox提供的用于样式设置()的表达式来更改颜色
let colorDictionary: [NSNumber: UIColor] = [
0.0: .clear,
0.01: .white,
0.15: UIColor(red: 0.19, green: 0.30, blue: 0.80, alpha: 1.0),
0.5: UIColor(red: 0.73, green: 0.23, blue: 0.25, alpha: 1.0),
1: .yellow
]
也许我们需要定义一些固定值,比如1=#db7851,2=。。。。等等来做吗
为了添加geojson数据,我们使用以下代码
let data = try Data(contentsOf: url)
guard let shapeCollectionFeature = try MGLShape(data: data, encoding: String.Encoding.utf8.rawValue) as? MGLShapeCollectionFeature else {
fatalError("Could not cast to specified MGLShapeCollectionFeature")
}
// Create source and add it to the map style.
let source = MGLShapeSource(identifier: "flurstuecke_shape", shape: shapeCollectionFeature, options: nil)
style.addSource(source)
let fillLayer = MGLFillStyleLayer(identifier: "flurstuecke", source: source)
style.addLayer(fillLayer)
出于测试目的,我们添加了一个用于更改所选功能颜色的触摸事件(仅用于测试MapBox表达式)
我们希望有人能给我们一个提示或一些文档,帮助我们为每个功能着色。谢谢:)使用
layer.lineColor=NSExpression(forKeyPath:“颜色”)
geojson颜色属性值示例:
“颜色”的值可以是:[“RGB”,255,0,0],“红色”,“000000”谢谢您的帮助。有时候就这么简单:)
let spot = sender.location(in: mapView)
let features = mapView.visibleFeatures(at: spot, styleLayerIdentifiers: Set(["flurstuecke"]))
if let feature = features.first, let fbid = feature.attribute(forKey: "FBID") as? String {
guard let layer = mapView.style?.layer(withIdentifier: "flurstuecke") as? MGLFillStyleLayer
else {
fatalError("Could not cast to specified MGLFillStyleLayer")
}
layer.fillColor = NSExpression(format: "TERNARY(FBID = %@, %@, %@)", fbid, UIColor.green, UIColor.blue)
}