单击后如何清除传单层

问题描述 投票:0回答:1

我是关于传单的新手,我尝试使用鼠标单击来选择/取消选择图层。首先mi地图就是这样

enter image description here

单击图层后,我想选择它并突出显示

enter image description here

现在,如果我再次单击previos select层,我想取消选择它并重置高光。这是我用来执行此操作的代码。

  onEachFeature: function(feature,layer) {

      layer.setStyle({fillOpacity: 0.0 , color: '#424a44', weight: 2});
      layer.on('click', function(e) {

      let isLayerAlreadySelected =  // Some logic to undestand if layer alreeady selected

      if(isLayerAlreadySelected) 
         layer.setStyle({fillOpacity: 0.0 , color: '#424a44', weight: 2});
      else
          layer.setStyle({fillOpacity: 0.4 , color: '#004691', weight: 3});
      }

  }

但是有时当我尝试取消选择先前未选择图层样式的图层时,不保留不透明度。有什么建议吗?

javascript angular leaflet
1个回答
0
投票

您可以测试图层具有哪种颜色:

onEachFeature: function(feature,layer) {

      layer.setStyle({fillOpacity: 0.0 , color: '#424a44', weight: 2});
      layer.on('click', function(e) {

      let isLayerAlreadySelected =  layer.options.color === '#004691';

      if(isLayerAlreadySelected) 
         layer.setStyle({fillOpacity: 0.0 , color: '#424a44', weight: 2});
      else
          layer.setStyle({fillOpacity: 0.4 , color: '#004691', weight: 3});
      }

  }
© www.soinside.com 2019 - 2024. All rights reserved.