Building predicate (NSExpression) to determine icon color for Mapbox SymbolStyle layer

1k Views Asked by At

I've recently started working with the Mapbox SDK on iOS and I've got some markers that I want to add to my map. Each marker represents an object that has several properties, among them: a type property (which backs to an enum) and a boolean property. I have a MGLSymbolStyleLayer defined and I've set most of the icon-related properties accordingly. Currently, I'm setting the icon color based solely on the "type" of the object like this:

let defaultColor = UIColor.blue
layer.iconColor = NSExpression(format: "MGL_MATCH(type, 'type1', %@, 'type2', %@, 'type3', %@, %@)", UIColor.orange, UIColor.purple, UIColor.yellow, defaultColor)

This is working well but now I need to add an additional piece of logic in the NSExpression to check the boolean property on each object and if it's true, then set the icon color to red; else, determine the icon color for each object based on it's type (using the logic defined above). So in a nutshell, I need an NSExpression that functions like this:

if feature.booleanProperty == true {
   // set icon color to red
} else {
   // set icon color based on the feature type
}

I've looked through the Mapbox Predicates and Expressions Reference doc but it's still not clear to me how I would implement this sort of conditional logic in an NSExpression. Can anyone tell me if this is possible to do and if so, how?

1

There are 1 best solutions below

2
On BEST ANSWER

From what I tried:

NSExpression(format: "TERNARY(booleanProperty=YES, %@, MGL_MATCH(type, 'type1', %@, 'type2', %@, 'type3', %@, %@))", UIColor.red, UIColor.orange, UIColor.purple, UIColor.yellow, defaultColor)

SIDE NOTE: I tried only compositing ternary operators and evaluating on object, not this specific expression.