我正在尝试使用mapbox-gl创建分区统计图。在分区统计图示例中,它们似乎根据要素的属性设置了要素的绘画填充颜色。有没有办法通过访问地图来设置颜色?
即。我有每个磁贴,在名为 id 的功能属性中都有一个唯一的 id。我还有一个 json,它用一个值映射每个 id,并希望访问这些值来设置颜色。
这可能吗? 还是我只能访问功能属性中的值?
我不完全确定我是否正确理解了你的问题。但我认为您要实现的目标可以通过表达式来完成:
const geojson = {
type: 'FeatureCollection',
features: [
{
type: 'Feature',
properties: {
id: 'foo'
},
geometry: {
/* */
}
}
]
};
const values = {
foo: 'green',
bar: 'red',
baz: 'blue'
};
map.addLayer({
// ...
paint: {
'fill-color': [
[
'get',
// get the id property and use it as a key into "values"
['get', 'id'],
values
]
]
}
});
请参阅get
表达式:https://www.mapbox.com/mapbox-gl-js/style-spec#expressions-get
从 04.2018 更新:从某个时刻开始,像这样的结构
[
'get',
// get the id property and use it as a key into "values"
['get', 'id'],
values
]
停下来工作。出现以下异常:"裸对象无效。使用 ["literal", {...}]'。现在有必要使用类型表达式,如下所示:
[
'get',
['string', ['get', 'id'],
['literal', values]
]
请参阅此和此以供参考。