地图框数据驱动样式.为不在对象中的值设置颜色



目前我正在这样设置油漆属性

this.map.setPaintProperty('da-highlighted', 'fill-color', 
                  // get the id property and use it as a key into "values"
                  ["get", ["to-string", ["get", "DAUID"]], ["literal", coloredDas]]);

因此,这对于 id 位于 coloredDas 对象中的磁贴来说效果很好。但它将未找到的颜色设置为黑色。无论如何,我可以为对象中未存在的值设置默认颜色吗?我希望它是透明的。不确定语法,或者是否可能。我在网上找不到有关此案的任何信息。

我根据答案尝试了以下方法

this.map.setPaintProperty('da-highlighted', 'fill-color', 
                  // get the id property and use it as a key into "values"
                    [
                    'case',
                    ['has', ["to-string", ['has', "DAUID"],["literal", coloredDas]], ["get", ["to-string", ["get", "DAUID"]], ["literal", coloredDas]],
                    'blue'
                    ]]  

              );

但收到以下错误:

Error: layers.da-highlighted.paint.fill-color: Expected at least 3 arguments, but found only 1.

我后来将查询格式化为:

                    [
                    'case',
                    ['has', ["to-string", ['has', "DAUID"],["literal", coloredDas]]], [["get", ["to-string", ["get", "DAUID"]], ["literal", coloredDas]]],
                    'blue'
                    ]

但是我当时得到的错误是

Error: layers.da-highlighted.paint.fill-color[1][1]: Expected arguments of type (value), but found (boolean, object) instead.

您可以尝试使用 casematch 表达式。将其与当前get相结合,并提供default值。像这样:

[
  'case',
  [
    'has',
    ['to-string', ['get', 'DAUID']],
    [
      'literal',
      colors
    ]
  ],
  [
    'get',
    ['to-string', ['get', 'DAUID']],
    [
      'literal',
      colors
    ]
  ],
  'white'
];

在此处查看文档:https://www.mapbox.com/mapbox-gl-js/style-spec#expressions-case

下面是一个 jsfiddle 演示circle-color油漆属性的表达式:https://jsfiddle.net/Scarysize/xwbxx3wq/14/

最新更新