如何使MGLSymbolStyleLayer动态化

问题描述

我已经在我的应用程序中实现了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

解决方法

因此解决方案是ports.text = NSExpression(forKeyPath: "name")

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...