1

我有特征集合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")
        }
    })
}
4

1 回答 1

2

您可能需要在样式中设置图像名称。这会将指定的图像添加到样式的图像中。

如果您想iconImageName根据 for 的值设置样式id,您可能还想尝试使用NSExpression(forKeyPath:)而不是NSExpression(forConstantValue:)。例如:

pointLayer.iconImageName = NSExpression(forKeyPath: id)

您可能会觉得有帮助的一些示例:

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

于 2019-07-31T18:14:49.040 回答