我在 Mapbox Studio 上创建了自定义地图样式,并上传了具有多个多边形特征的 GeoJSON,该文件如下所示:
{
"type": "FeatureCollection",
"name": "AD_micro-regions",
"crs": {
"type": "name",
"properties": {
"name": "urn:ogc:def:crs:OGC:1.3:CRS84"
}
},
"features": [
{
"type": "Feature",
"id": "AD-01_low_high",
"properties": {
"id": "AD-01",
"elevation": "low_high"
},
"geometry": {
"type": "MultiPolygon",
"coordinates": [...]
}
},
... other fetures
]
我为创建的图层定义了自定义样式,以便每个要素都根据
danger_level
要素状态(1、2、3、4、5)进行着色:
[
"match",
["feature-state", "danger_level"],
[1],
"rgb(134, 231, 75)",
[2],
"rgb(249, 219, 73)",
[3],
"rgb(235, 134, 52)",
[4],
"rgb(216, 69, 49)",
[5],
"rgb(184, 64, 46)",
"rgba(0, 0, 0, 0)"
]
现在我的计划是为 iOS SDK 中的每个功能设置
danger_level
,但我真的找不到方法来做到这一点。
我尝试使用
setFeatureState
但没有效果:
map.setFeatureState(
sourceId: "composite",
//sourceLayerId: "micro-regions_elevation-2p16jz",
featureId: "AD-01_low_high",
state: ["danger_level": 4],
callback: { result in
// This returns `success(<null>)`
print(result)
map.getFeatureState(
sourceId: "composite",
featureId: "AD-01_low_high",
callback: { state in
// This correctly prints `success(["danger_level": 4])`
// but the map style is not updated
print(state)
}
)
}
)
上面的代码没有效果,状态似乎设置正确,但渲染的地图上没有发生更新。
如果我还指定
sourceLayerId
,第二个 print
语句将返回一个空字典。
如果我打印
map.allSourceIdentifiers
我只会看到这个:
[
MapboxMaps.SourceInfo(id: "composite", type: MapboxMaps.SourceType(rawValue: "vector")),
MapboxMaps.SourceInfo(id: "6D9AE", type: MapboxMaps.SourceType(rawValue: "geojson"))
]
我的地图有两个 geojson 图层,而不是一个,6D9AE 会在每次渲染时更改 id。我尝试使用
map.allSourceIdentifiers.last!.id
作为 sourceId
进行 setFeatureState
调用,但没有效果。
我尝试使用
map.layer(withId: "micro-regions_elevation")
检索图层,它正确返回了图层数据:
FillLayer(id: "micro-regions_elevations", type: MapboxMaps.LayerType(rawValue: "fill"), filter: nil, source: Optional("composite"), sourceLayer: Optional("micro-regions_elevation-2p16jz"), slot: nil, minZoom: nil, maxZoom: nil, visibility: MapboxMaps.Value<MapboxMaps.Visibility>.constant(MapboxMaps.Visibility.visible), fillSortKey: nil, fillAntialias: nil, fillColor: Optional(MapboxMaps.Value<MapboxMaps.StyleColor>.expression([match, [get, danger_level], 1, [rgba, 134.0, 231.00001525878906, 75.0, 1.0], 2, [rgba, 249.00001525878906, 219.00001525878906, 73.0, 1.0], 3, [rgba, 235.00001525878906, 134.0, 52.000003814697266, 1.0], 4, [rgba, 216.00001525878906, 69.0, 49.000003814697266, 1.0], 5, [rgba, 184.0, 64.0, 46.0, 1.0], [rgba, 0.0, 0.0, 0.0, 0.0]])), fillColorTransition: nil, fillEmissiveStrength: nil, fillEmissiveStrengthTransition: nil, fillOpacity: Optional(MapboxMaps.Value<Swift.Double>.constant(0.5)), fillOpacityTransition: nil, fillOutlineColor: Optional(MapboxMaps.Value<MapboxMaps.StyleColor>.constant(MapboxMaps.StyleColor(rawValue: "rgba(0.00, 0.00, 0.00, 0.00)"))), fillOutlineColorTransition: nil, fillPattern: nil, fillTranslate: nil, fillTranslateTransition: nil, fillTranslateAnchor: nil)
但我仍然得到相同的结果。
我也尝试了这种替代填充颜色公式,但结果相同:
[
"case",
["==", ["feature-state", "danger_level"], 1],
"rgb(134, 231, 75)",
["==", ["feature-state", "danger_level"], 2],
"rgb(249, 219, 73)",
["==", ["feature-state", "danger_level"], 3],
"rgb(235, 134, 52)",
["==", ["feature-state", "danger_level"], 4],
"rgb(216, 69, 49)",
["==", ["feature-state", "danger_level"], 5],
"rgb(184, 64, 46)",
"rgba(0, 0, 0, 0)"
]
我做错了什么?我正在使用 SDK 11.0.0-rc.1
调用以下代码使用过滤器更新图层:
mapView.mapboxMap.style.updateLayer(withId:type:update:)
来自 Mapbox github 的示例
使用表达式并首先在Mapbox Studio上测试它们
func applyMYFilter(for types: [String]) throws {
try updateMapLayer(withId: "poi-label", ofType: SymbolLayer.self) { layer in
layer.filter = Expression.match {
Expression.get(.string("class")),
types,
true,
false
}
}
}
func updateMapLayer(withId layerId: String, ofType layerType: SymbolLayer.Type, updateHandler: (inout SymbolLayer) throws -> Void) throws {
try mapView.mapboxMap.style.updateLayer(withId: layerId, type: layerType) { layer in
try updateHandler(&layer)
}
}