You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mapbox case表达式报错求助:paint.fill-color参数数量异常

Fixing the "Expected an odd number of arguments" Error in Mapbox Case Expression

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 case expression. This handles any edge cases where none of your conditions match (e.g., if max_expr returns 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:06:35