Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
287 views
in Technique[技术] by (71.8m points)

How do I combine Mapbox mgl_interpolate and MGL_Match in an NSExpression?

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]])

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)
          ]

zoom 9

zoom 16


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...