问题描述
我已经在我的应用程序中实现了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")