Building an expression dynamically using Mapbox Maps iOS SDK v10?

124 views Asked by At

I'm using the Mapbox Maps iOS SDK v10 in an app and I have a layer that renders circles on the map which represent the location of various features. Each feature has several properties including a string "type" like "CAR", "BOAT", "AIRCRAFT", etc. I'm using a color expression to determine what color each circle should be based on the "type" value of the feature. Currently, that expression is constructed like this:

let colorExpression = Exp(.match) {
    Exp(.get) { "type" }
    "CAR"
    UIColor.systemBlue
    "BOAT"
    UIColor.systemRed
    "AIRCRAFT"
    UIColor.systemGreen
    UIColor.systemGray // default color if some other "type" value
}

This worked well enough initially because there were a limited number of values for a feature's "type" property so hard coding those values was a workable solution. However, the feature data comes from a server and, from time to time, new feature "types" are introduced. So instead of needing to add more hard-coded type values and make a new build of the app every time this happens (which I don't always know about in advance), I'd like to be able to build this expression "dynamically" using data that I get back from the API call to the server.

For example, assume that post-deserialization, I have types and UIColor values in a dictionary like this:

var typeColors: [String: UIColor] = [
   "CAR" : UIColor.systemBlue,
   "BOAT" : UIColor.systemRed,
   "AIRCRAFT" : UIColor.systemGreen,
   "BICYCLE" : UIColor.systemYellow
]

I would like to iterate that dictionary and construct the color expression using those key value pairs. Something along the lines of:

var typeColorArray: [Any] = []
for (k, v) in typeColors {
    typeColorArray.append(k)
    typeColorArray.append(v)
}

let colorExpression = Exp(.match) {
    Exp(.get) { "type" }
    typeColorArray
    UIColor.systemGray // default color if some other "type" value
}

The Mapbox iOS SDK docs are not terribly verbose with regard to the expression DSL and I couldn't find an example quite like this in the examples hosted in the GitHub repo (https://github.com/mapbox/mapbox-maps-ios/tree/main/Apps/Examples/Examples/All%20Examples). Does anyone know if it's possible to create a color expression like this?

0

There are 0 answers