首页 > 解决方案 > Mapbox 中的“step”表达式是如何工作的?

问题描述

步进函数如何在数组的 Mapbox 填充颜色属性中工作?尽管:

R=[ 'interpolate', ['linear'],['number',['get', dim_properties.name]], -150, "#800026", -133, "#bd0026", -116, "#e31a1c", -100, "#fc4e2a", -83, "#fd8d3c", -66, "#feb24c", -50, "#fed976", -33, "#ffeda0", -16, "#ffffcc", 0, "#ffffff"]

map.addLayer({
            id: 'er',
            type: 'fill',
            source: {
              type: 'vector',
              url: pixelling_url
            },
            'source-layer':pixelling_source_layer,
                paint: {

        'fill-color':R
    }

完美运行,

这个其他代码没有。

R=[ 'step',['get', dim_properties.name]], -150, "#800026", -133, "#bd0026", -116, "#e31a1c", -100, "#fc4e2a", -83, "#fd8d3c", -66, "#feb24c", -50, "#fed976", -33, "#ffeda0", -16, "#ffffcc", 0, "#ffffff"]

map.addLayer({
            id: 'er',
            type: 'fill',
            source: {
              type: 'vector',
              url: pixelling_url
            },
            'source-layer':pixelling_source_layer,
                paint: {

        'fill-color':R
    }

错误消息是:“paint.fill-color: Expected an even number of arguments

请注意,两段代码之间的区别仅在于 R 的定义。

标签: mapbox-gl-js

解决方案


使用步骤表达式,您需要设置一个基值。如果愿意,只需删除第一个中断值即可解决您的问题。现在 Mapbox 在查找四个参数(表达式类型、属性值、基值、断点集合)时只读取三个参数(表达式类型、属性、断点集合)。基本上,您不需要定义最小值。Mapbox GL 将推断应该将基值分配给第一个断点以下的任何要素。在这种情况下,它将是满足条件的任何特征dim_properties.name < -133

    R = [
      'step', // arg 1
      ['get', 'dim_properties.name'], // arg 2
      '#800026', // arg 3
      -133, '#bd0026', // rest of the expression is arg 4
      -116, '#e31a1c',
      -100, '#fc4e2a',
      -83, '#fd8d3c',
      -66, '#feb24c',
      -50, '#fed976',
      -33, '#ffeda0',
      -16, '#ffffcc',
      0, '#ffffff'
     ]

推荐阅读