admin 管理员组文章数量: 1086019
I have a map of states with each precinct as an individual feature in Mapbox. I want to change the color of each of these precincts based on the difference between party votes. Here's an example of what I'm trying to achieve versus what I have right now.
I tried using map.setPaintProperty, however setting one feature forces you to change every other feature that doesn't match the ID.
map.setPaintProperty("wisconsin", "fill-color",
["match",["get", "id"],
features[i].properties["OBJECTID"],
color, "#ffffff"
]
);
How could I go about this or is there another method of doing so?
I have a map of states with each precinct as an individual feature in Mapbox. I want to change the color of each of these precincts based on the difference between party votes. Here's an example of what I'm trying to achieve versus what I have right now.
I tried using map.setPaintProperty, however setting one feature forces you to change every other feature that doesn't match the ID.
map.setPaintProperty("wisconsin", "fill-color",
["match",["get", "id"],
features[i].properties["OBJECTID"],
color, "#ffffff"
]
);
How could I go about this or is there another method of doing so?
Share Improve this question asked Sep 2, 2020 at 21:21 Drew GiffordDrew Gifford 531 silver badge3 bronze badges1 Answer
Reset to default 9So, you want to do data-driven styling. There are basically three methods.
Expression mapping property to color
If the attribute you want to visualise is in the data (ie, you have a .population
property on each feature), you use an expression like:
'fill-color': ['interpolate-hcl', ['linear'], ['get', 'population'], 0, 'red', 1e6, 'blue']
See https://docs.mapbox./help/glossary/data-driven-styling/
Expression mapping each ID to a specific color
If your attributes are not contained within your data source, you can programmatically generate a huge expression that will look like:
'fill-color': ['match', ['get', 'id'],
1020, 'red',
1033, 'green',
1038, 'red',
1049, 'white',
// ...
Use feature-state to add the attribute at run-time
Finally, you can get better rendering performance than the previous case, albeit with some extra plexity, by calling setFeatureState
to actually add the attribute you want at runtime.
Your style looks like this:
'fill-color': ['interpolate-hcl', ['linear'], ['feature-state', 'population'], 0, 'red', 1e6, 'blue']
Then you iterate over every feature within the viewport to actually add that attribute:
for (const district of districts) {
map.setFeatureState({ source: 'mysource', sourceLayer: 'mysourcelayer', id: district.id }, { population: district.population});
}
See https://docs.mapbox./mapbox-gl-js/api/map/#map#setfeaturestate .
本文标签: javascriptMapbox GL JS Coloring individual features in large GeoJSONStack Overflow
版权声明:本文标题:javascript - Mapbox GL JS: Coloring individual features in large GeoJSON - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744086920a2531299.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论