Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/111.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 功能集合中每个功能的不同图标_Ios_Swift_Mapbox_Mapbox Ios - Fatal编程技术网

Ios 功能集合中每个功能的不同图标

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)

我有功能集合geojson。我想为它们的属性设置不同的图标基。但我不知道怎么做。现在我只能为所有层设置一个图像。是否可以将每个功能设置为不同的图标

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)

您可能会发现一些有用的示例:

此外,您可能希望将源和图层添加到主线程上的样式中。在背景线程上添加样式层可能会导致意外行为