How to make MGLSymbolStyleLayer dynamic

133 views Asked by At

I have implemented an MGLPointFeatureCluster in my app and everything is working well, but when I zoom in, the MGLSymbolStyleLayer name takes the last name of the array. I tried everything but still, it does not want to work.

 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)
        }
    }
 }

Before Zooming [1]: https://i.stack.imgur.com/2bRz1.jpg

After Zooming [2]: https://i.stack.imgur.com/r5sZg.png

1

There are 1 answers

0
ASH On

So the solution is ports.text = NSExpression(forKeyPath: "name")