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

时间:2017-08-14 09:33:43

标签: javascript mapbox-gl-js

我想在悬停时突出显示我的地图样式中包含的图层的特定功能..这样的事情:

  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]);

它做了我想做的事,但它让我添加另一层,我不想要那样。我想在我的地图样式中使用图层。就像第一个代码一样。 怎么用简单的代码呢?

2 个答案:

答案 0 :(得分:2)

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

,语法为

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

答案 1 :(得分:0)

我使用了这段代码:

    var highlight =   {
           property: 'NAME_2',
           type: "categorical",
           stops: [[MunicipalName, 'blue']]
         };
   map.setPaintProperty('n_Region6 Municipals', 'fill-color', highlight); 

现在的问题是我没有考虑到它会为其余的多边形黑色着色。有没有简单的方法如何在不添加更多图层的情况下突出显示单个特征? Highlights blue but change all the colors