Ios 功能集合中每个功能的不同图标
我有功能集合geojson。我想为它们的属性设置不同的图标基。但我不知道怎么做。现在我只能为所有层设置一个图像。是否可以将每个功能设置为不同的图标Ios 功能集合中每个功能的不同图标,ios,swift,mapbox,mapbox-ios,Ios,Swift,Mapbox,Mapbox Ios,我有功能集合geojson。我想为它们的属性设置不同的图标基。但我不知道怎么做。现在我只能为所有层设置一个图像。是否可以将每个功能设置为不同的图标 func drawPoint(geoJson : String , id: String) { DispatchQueue.global(qos: .background).async(execute: { do { let data = geoJson.data(using: .utf8)
func drawPoint(geoJson : String , id: String) {
DispatchQueue.global(qos: .background).async(execute: {
do {
let data = geoJson.data(using: .utf8)
let id = "kgm-\(id)"
guard let shapeCollectionFeature = try MGLShape(data: data!, encoding: String.Encoding.utf8.rawValue) as? MGLShapeCollectionFeature else {
fatalError("Could not cast to specified MGLShapeCollectionFeature")
}
let source = MGLShapeSource(identifier: id, shape: shapeCollectionFeature, options: nil)
self.mapView.style?.addSource(source)
let pointLayer = MGLSymbolStyleLayer(identifier: id, source: source)
let zoomStops = [
13.49: NSExpression(forConstantValue: 0),
13.5: NSExpression(forConstantValue: 1)
]
pointLayer.iconOpacity = NSExpression(format: "mgl_interpolate:withCurveType:parameters:stops:($zoomLevel, 'linear', nil, %@)", zoomStops)
pointLayer.iconImageName = NSExpression(forConstantValue: id)
pointLayer.iconAllowsOverlap = NSExpression(forConstantValue: true)
self.mapView.style!.addLayer(pointLayer)
} catch {
print("GeoJSON parsing failed")
}
})
}
你可能需要改变一下你的风格。这会将指定的图像添加到样式的图像中
如果要根据id
的值将样式设置为iconImageName
,您还可以尝试使用NSExpression(forKeyPath:)
而不是NSExpression(forConstantValue:)
。例如:
pointLayer.iconImageName=NSExpression(forKeyPath:id)
您可能会发现一些有用的示例: