Reputation: 1178
I'm having difficulty figuring out how to apply a match to contour index values in conjunction with different zoom levels using Swift in iOS to style different line widths, depending on the index value. Starting @ zoom level 9, lines with an index value of 10 are meant to be 1.5, those with 5 are 1 and all others default to 0.5. Here is the JSON equivalent:
[
"interpolate",
["linear"],
["zoom"],
9,
[
"match",
["get", "index"],
[10],
1.5,
[5],
1,
0.5
],
16,
[
"match",
["get", "index"],
[10],
3,
[5],
2,
1
]
]
I understand how I'd do this if it was simply a question all lines being the same widths:
layer.lineWidth = NSExpression(format: "mgl_interpolate:withCurveType:parameters:stops:($zoomLevel, 'linear', nil, %@)", [9: 1, 16: 2])
Is there a way I would replace the 1 and the 2 (after the 9 and 16, respectively) with another NSExpression containing MGL_MATCH, e.g:
let lineWidthStops = [
NSExpression(format: "MGL_MATCH(index, 10, %@, 5, %@, %@)", 1.5, 1.0, 0.5),
NSExpression(format: "MGL_MATCH(index, 10, %@, 5, %@, %@)", 3.0, 1.5, 1.0)
]
contourLayer.lineWidth = NSExpression(format: "mgl_interpolate:withCurveType:parameters:stops:($zoomLevel, 'linear', nil, %@)", [9: lineWidthStops[0], 16: lineWidthStops[1]])
Upvotes: 2
Views: 412
Reputation: 321
Yes, you can reference the array of NSExpressions within your interpolation expression, however, you will need to change the string formatter %@
in your MGL_MATCH expressions to floating point number specifiers %f
as in the code below:
let lineWidthStops = [
NSExpression(format: "MGL_MATCH(index, 10, %f, 5, %f, %f)", 1.5, 1.0, 0.5),
NSExpression(format: "MGL_MATCH(index, 10, %f, 5, %f, %f)", 5.0, 1.5, 1.0)
]
Upvotes: 2