The issue we are tackling is that we need to add some filtering on the cluster by a specific attribute for all objects in a cluster.
The code before that demand is:
// Create style layer to cluster features as images with labels
let clusterLayer = MGLSymbolStyleLayer(identifier: "clusteredCarsLayer", source: carsSource)
clusterLayer.textColor = NSExpression(forConstantValue: UIColor.white)
clusterLayer.textHaloColor = NSExpression(forConstantValue: UIColor.withAlphaComponent(UIColor(hexString: "000000"))(0.4))
clusterLayer.textHaloWidth = NSExpression(forConstantValue: 20)
clusterLayer.textAnchor = NSExpression(forConstantValue: "bottom")
clusterLayer.textOffset = NSExpression(forConstantValue: NSValue(cgVector: CGVector(dx: 0, dy: 2)))
clusterLayer.textRadialOffset = nil
clusterLayer.textFontSize = NSExpression(forConstantValue: NSNumber(value: Double(self?.icon.size.width ?? 0) / 2))
clusterLayer.iconAllowsOverlap = NSExpression(forConstantValue: false)
clusterLayer.iconImageName = NSExpression(forConstantValue: "carsCluster")
style.setImage(UIImage(named: "CarIconCluster")!, forName: "carsCluster")
let stops = [
2: NSExpression(forConstantValue: "carsCluster"),
]
let defaultShape = NSExpression(forConstantValue: "car")
clusterLayer.iconImageName = NSExpression(format: "mgl_step:from:stops:(point_count, %@, %@)", defaultShape, stops)
clusterLayer.text = NSExpression(format: "CAST(point_count, 'NSString')")
style.addLayer(clusterLayer)
As you can see, this is working, Creating a cluster if there are more than two cars with the number of cars under the cluster icon.
Now, The demand is to cluster by the status of the cars (status is an attribute that every car object has). For example, If all cars are in Final status then set image to Final image, otherwise set image to Active image.
I know how to filter for a specific car just like I did here:
// Show unclustered features as icons. The `cluster` attribute is built into clustering-enabled source features.
// This example requires two style layers to work properly: one for clustered points and one for unclustered points
let markerLayer = MGLSymbolStyleLayer(identifier: "carsLayer", source: carsSource)
//markerLayer.iconImageName = NSExpression(forConstantValue: "car")
markerLayer.predicate = NSPredicate(format: "cluster != YES")
style.addLayer(markerLayer)
// Create a stops dictionary with keys that are possible values for 'status', paired with icon images that will represent those features.
style.setImage(UIImage(named: "CarIconFinal")!, forName: "carFinal")
style.setImage(UIImage(named: "CarIconActive")!, forName: "carActive")
style.setImage(UIImage(named: "CarIconSelected")!, forName: "carActiveSelected")
style.setImage(UIImage(named: "CarIconSelectedInactive")!, forName: "carFinalSelected")
let carIcons = ["Final" : "carFinal", "Active" : "carActive"]
// Use the stops dictionary to assign an icon based on the "status" for each feature.
markerLayer.iconImageName = NSExpression(format: "FUNCTION(%@, 'valueForKeyPath:', status)", carIcons)
But Both IOS and Android teams can't figure this out for native implementation for MapBox SDK, just can't figure out or find anywhere how to do it for the cluster, As we need to look into all the objects in the cluster and set a different image for deferent status for all the values for a specific attribute (As I wrote before, ONLY if the status for ALL objects in a cluster has a specific value than set the image for a specific image).
Any Idea, Direction?