gpt4 book ai didi

mapbox-ios - 通过颜色特征属性设置 MGLFillStyleLayer 的 FillColor

转载 作者:行者123 更新时间:2023-12-02 02:45:09 25 4
gpt4 key购买 nike

我们正在开发一个 iOS 应用程序并且在设置 MGLFillStyleLayer (MapBox iOS SDK) 的填充颜色时遇到问题。

我们有一个大的 .geojson 文件,它被解析并添加到标准 MapBox map 中。 geojson 中的每个要素都有一个“颜色”属性来设置要素的背景颜色。颜色保存为十六进制代码。

是否可以使用 MapBox 表达式或“forEach 功能 -> 设置填充颜色”之类的东西单独设置每个功能的颜色?

我们尝试使用 MapBox 提供的样式表达式( https://docs.mapbox.com/ios/api/maps/4.1.1/for-style-authors.html )来更改颜色,但无法弄清楚如何将特征属性加载到 swift 函数中以生成颜色。
在 Mapbox ( https://docs.mapbox.com/ios/maps/examples/heatmap-example/ ) 的热图示例中,我们已经看到可以通过 NSNumber-Value 设置填充颜色

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 表达式)。
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)
}

我们希望有人可以给我们一个提示或一些文档来帮助我们为每个功能着色。谢谢 :)

最佳答案



layer.lineColor = NSExpression(forKeyPath: "color")



geojson 颜色属性值示例:
"color"的值可以是:["RGB",255,0,0],"red","#000000"

关于mapbox-ios - 通过颜色特征属性设置 MGLFillStyleLayer 的 FillColor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55653003/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com