Как сделать MGLSymbolStyleLayer динамическим

#mapbox #mapbox-ios

#mapbox #mapbox-ios

Вопрос:

Я внедрил an MGLPointFeatureCluster в свое приложение, и все работает хорошо, но когда я увеличиваю масштаб, MGLSymbolStyleLayer имя принимает последнее имя массива. Я перепробовал все, но, тем не менее, он не хочет работать.

  func mapView(_ mapView: MGLMapView, didFinishLoading style: MGLStyle) {
    guard let path = Bundle.main.path(forResource: "data", ofType: "geojson") else {return}
    let url = URL(fileURLWithPath: path)

    let source = MGLShapeSource(identifier: "clusteredPorts", url: url, options: [.clustered: true, .clusterRadius: icon.size.width])
    style.addSource(source)
    style.setImage(icon?.withRenderingMode(.alwaysTemplate) ?? .init(), forName: "icon")
    
    LocalFileManager().decodeFile(fileURL: url) { (results) in
        results.features.forEach { (feature) in
            let ports = MGLSymbolStyleLayer(identifier: "ports(feature.properties.name)", source: source)
            ports.text = NSExpression(forConstantValue: feature.properties.name)
            ports.iconColor = NSExpression(forConstantValue: UIColor.red)
            ports.textColor = NSExpression(forConstantValue: UIColor.white)
            ports.predicate = NSPredicate(format: "cluster != YES")
            ports.iconAllowsOverlap = NSExpression(forConstantValue: true)
            style.addLayer(ports)
        }
    }
 }
  

Перед масштабированием
[1]: https://i.stack.imgur.com/2bRz1.jpg

После масштабирования [2]: https://i.stack.imgur.com/r5sZg.png

Ответ №1:

Итак, решение таково ports.text = NSExpression(forKeyPath: "name")