Cor*_*son 3 properties paint layer fill mapbox-gl-js
是否可以根据文本属性而不是数值来设置填充颜色停止点
例如根据省名填写
我的输入数据集有一个名为 PROV_ID 的属性/列,并包含每个州/省的 2 个字母的 ID
所以我的目标是:'stops': [['GP', 'YELLOW']]
但是,当如下所示实现时,代码不会呈现任何填充颜色,我已将下面代码中的 PROV_ID 替换为要测试的主键属性 [numeric],并且效果很好
我想问题是如果填充颜色停止仅限于数字属性?
map.addLayer({
'id': 'countiesLayer',
'type': 'fill', /*define the type of layer fill, line, point, fill-extrusion, background, raster, circle*/
'source': 'mySrcName',
'source-layer': '3_Fields-83vr21',
'layout': {
'visibility': 'visible'
},
/*there are many options for styling - this is a simple style*/
'paint': {
'fill-color': {
'property': 'PROV_ID',
'stops': [['GP', 'YELLOW']]
},
'fill-outline-color': 'white'
}
});
Run Code Online (Sandbox Code Playgroud)
我认为您需要一个带有match函数的表达式。
代码将是(未测试):
map.addLayer({
'id': 'countiesLayer',
'type': 'fill', /*define the type of layer fill, line, point, fill-extrusion, background, raster, circle*/
'source': 'mySrcName',
'source-layer': '3_Fields-83vr21',
'layout': {
'visibility': 'visible'
},
/*there are many options for styling - this is a simple style*/
'paint': {
'fill-color': ['match', ['get', 'PROV_ID'], // get the property
'GP', 'yellow', // if 'GP' then yellow
'XX', 'black', // if 'XX' then black
'white'] // white otherwise
},
'fill-outline-color': 'white'
}
});
Run Code Online (Sandbox Code Playgroud)
从文档:
比赛
选择标签值与输入值匹配的输出,如果未找到匹配,则选择回退值。输入可以是任何字符串或数字表达式(例如 ["get", "building_type"])。每个标签可以是单个文字值或值数组。
Run Code Online (Sandbox Code Playgroud)["match", input: InputType (number or string), label_1: InputType | [InputType, InputType, ...], output_1: OutputType, label_n: InputType | [InputType, InputType, ...], output_n: OutputType, ..., default: OutputType ]: OutputType