我有一个带有一些聚类点的地图视图(mapbox),该聚类是根据geojson进行排列和显示的,该聚类的逻辑就像它具有邻居一样,在缩小地图时会聚类。
这很好用,但是,我需要在NSExpression
内提取值,但没有找到解决方法。这种方法有什么通用做法吗?
我以这个例子为基础。 https://docs.mapbox.com/ios/maps/examples/clustering/
我已经对此进行了修改,因此簇的大小取决于簇中的点数,但是,当有很多点时,在120点范围内的某个地方,簇变得太大,因此我提取{{ 1}}是这样,我可以用它来设置一些规则。另外,如果您对我的问题有更好的解决方案,请分享。
point_count
我希望从 let url = URL(fileURLWithPath: Bundle.main.path(forResource: "ports", ofType: "geojson")!)
let source = MGLShapeSource(identifier: "clusteredPorts",
url: url,
options: [.clustered: true, .clusterRadius: icon.size.width])
style.addSource(source)
// Use a template image so that we can tint it with the `iconColor` runtime styling property.
style.setImage(icon.withRenderingMode(.alwaysTemplate), forName: "icon")
// Show unclustered features as icons. The `cluster` attribute is built into clustering-enabled
// source features.
let ports = MGLSymbolStyleLayer(identifier: "ports", source: source)
ports.iconImageName = NSExpression(forConstantValue: "icon")
ports.iconColor = NSExpression(forConstantValue: UIColor.darkGray.withAlphaComponent(0.9))
ports.predicate = NSPredicate(format: "cluster != YES")
ports.iconAllowsOverlap = NSExpression(forConstantValue: true)
style.addLayer(ports)
// Color clustered features based on clustered point counts.
let stops = [
20: UIColor(red: 0, green: 124/255, blue: 1, alpha: 1),
50: UIColor(red: 0, green: 124/255, blue: 1, alpha: 1)
]
// Show clustered features as circles. The `point_count` attribute is built into
// clustering-enabled source features.
let circlesLayer = MGLCircleStyleLayer(identifier: "clusteredPorts", source: source)
circlesLayer.circleOpacity = NSExpression(forConstantValue: 0.75)
circlesLayer.circleStrokeColor = NSExpression(forConstantValue: UIColor.white.withAlphaComponent(1.0))
circlesLayer.circleStrokeWidth = NSExpression(forConstantValue: 3)
circlesLayer.circleColor = NSExpression(format: "mgl_step:from:stops:(point_count, %@, %@)", UIColor(red: 0, green: 124/255, blue: 1, alpha: 1), stops)
circlesLayer.circleRadius = NSExpression(format: "CAST(point_count / 2, 'NSNumber')")
circlesLayer.predicate = NSPredicate(format: "cluster == YES")
style.addLayer(circlesLayer)
// Label cluster circles with a layer of text indicating feature count. The value for
// `point_count` is an integer. In order to use that value for the
// `MGLSymbolStyleLayer.text` property, cast it as a string.
let numbersLayer = MGLSymbolStyleLayer(identifier: "clusteredPortsNumbers", source: source)
numbersLayer.textColor = NSExpression(forConstantValue: UIColor.white)
numbersLayer.textFontSize = NSExpression(forConstantValue: NSNumber(value: Double(icon.size.width) / 2))
numbersLayer.iconAllowsOverlap = NSExpression(forConstantValue: true)
// THIS IS WHERE I WANT TO EXTRACT THE VALUE, from "point_count" to Int.
// `numbersLayer.text = NSExpression(format: "CAST(point_count, 'NSString')")`
numbersLayer.predicate = NSPredicate(format: "cluster == YES")
style.addLayer(numbersLayer)
中提取point_count作为In