Mapbox case表达式报错求助:paint.fill-color参数数量异常
Let's break down what's going wrong here and how to fix it:
The Root Cause
Your case expression is missing a default fallback value, which is required for Mapbox's case syntax. Here's why the error pops up:
Mapbox expects the case expression to follow this structure after the 'case' keyword:[condition1, result1, condition2, result2, ..., defaultResult]
This means the total number of arguments (excluding the 'case' itself) must be odd: pairs of condition+result, plus one final default value. Your current code only has 4 condition-result pairs (8 arguments total, even), so Mapbox throws the error asking for an odd number.
Fixed Code
First, we'll add a default color for any unexpected values of max_expr. We can also simplify the duplicate red cases using the in expression to clean things up:
const max_expr = ['max', ['get', 'POLY_COLOUR_CODE_D'], ['get', 'POLY_COLOUR_CODE_BW'], ['get', 'POLY_COLOUR_CODE_A']]; const layerStyle = { 'paint': { 'fill-color': [ 'case', ['==', max_expr, 1], 'green', ['==', max_expr, 2], 'yellow', ['in', max_expr, 3, 4], 'red', // Combine 3 and 4 into one condition '#888888' // Default fallback color for other values ] } };
Key Notes
- Default Value Requirement: Always include a default value at the end of your
caseexpression. This handles any edge cases where none of your conditions match (e.g., ifmax_exprreturns 0, 5, or null). - Simplifying Conditions: Using
['in', value, option1, option2, ...]is a cleaner way to check if a value matches any of multiple options, instead of writing separate==conditions with the same result.
内容的提问来源于stack exchange,提问作者Konrad Woj

