gpt4 book ai didi

javascript - Mapbox GL - 使用 map 样式在悬停时切换突出显示特定功能

转载 作者:行者123 更新时间:2023-11-29 21:02:47 25 4
gpt4 key购买 nike

我想在鼠标悬停时突出显示包含在我的 map 样式中的图层的特定功能......像这样:

  map.on('mousemove', function(e) {
var states = map.queryRenderedFeatures(e.point, {
layers: ['n_Region6 Municipals']
});
if (states.length > 0) {
map.setPaintProperty('n_Region6 Municipals', 'fill-color','
{"property":"NAME_2","type":"category","stops": +
[[states[0].properties.NAME_2 +,"blue"]]}');
});

将 setpaintproperty 与数据驱动功能配对后,我运气不佳。我尝试使用过滤器。

      map.addLayer({
"id": "state-fills",
"type": "fill",
"source": {
"type": "vector",
"url": "mapbox://noeltech.c8nzzthb"
},
"source-layer":"R6_Pop_byMunicipal-5cqj12",
"layout": {},
"paint": {
"fill-color": "#627BC1",
"fill-opacity": 0
}
});
map.addLayer({
"id": "state-fills-hover",
"type": "line",
"source": {
"type": "vector",
"url": "mapbox://noeltech.c8nzzthb"
},
"source-layer":"R6_Pop_byMunicipal-5cqj12",
"layout": {},
"paint": {
"line-color": "#627BC1",
"line-width": 3
},
"filter": ["==", "NAME_2", ""]
});
// map.setLayoutProperty('n_Region6 Municipals', 'visibility', 'none');

});
map.on("mousemove", "state-fills", function(e) {
map.setFilter("state-fills-hover", ["==", "NAME_2",
e.features[0].properties.NAME_2]);

它做了我想做的事,但它让我添加了另一层,我不想要那个。我想在我的 map 样式中使用图层。就像第一个代码一样。如何用简单的代码做到这一点?

最佳答案

你需要使用"type":"categorical"

语法是

map.setPaintProperty('n_Region6 Municipals', 'fill-color',{
"property":"NAME_2",
"type":"categorical",
"stops":[
[states[0].properties.NAME_2,"blue"],
],
"default": "red"
});

关于javascript - Mapbox GL - 使用 map 样式在悬停时切换突出显示特定功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45671506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com