Reputation: 1
I have a map view(mapbox) with some cluster points, the cluster is arranged and displayed based on a geojson, the logic of the cluster as if it has neighbours they will cluster when the map is zoomed out.
This works great, however, I need to extract value within an NSExpression
, but I'm finding no workaround. Are there any common practises for such methods?
I'm using this example as my base. https://docs.mapbox.com/ios/maps/examples/clustering/
I have modified this so the cluster size depends on the number of points in the cluster, however, when there many points, somewhere in the range out 120 points, the cluster gets too big, so my reason for extracting the point_count
is so I can use it to set some rules. Also, if you have a better solution to my problem, please do share.
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)
I'm expecting to extract point_count from NSExpression(format: "CAST(point_count, 'NSNumber')")
as an In
Upvotes: 0
Views: 420