1

I have layer(lote_ocupa) in control layers, when i launch application is disable but:

When I click on the map, even if the "lote_ocupa" layer is disabled, the identify function selects the feature, but that is wrong.

here is the part of the code:

var identifiedFeature;


mymap.on('click', function (e) {
    if(identifiedFeature){
      mymap.removeLayer(identifiedFeature);

    }
    lote_ocupa.identify().on(mymap).at(e.latlng).run(function(error, featureCollection){

      identifiedFeature = new L.GeoJSON(featureCollection.features[0], {
        style: function(){
          return {
            color: '#5C7DB8',
            weight: 2
          };
        }
      }).addTo(mymap);

    });
  });

How do fix this ?

Stephen Lead
  • 21,119
  • 17
  • 113
  • 240
krekto
  • 123
  • 4

2 Answers2

1

Assuming that by "disabled" you mean not visible, you need to tell the click function to exit if the layer isn't currently visible on the map. You can use hasLayer for this.

Try something like this:

mymap.on('click', function (e) {
  if (!mymap.hasLayer(lote_ocupa)) {
    return;
  }
  .....
});
Stephen Lead
  • 21,119
  • 17
  • 113
  • 240
0

thank you very much.

When I do it as you have shown:

if (!mymap.hasLayer(lote_ocupa)) {
    return;
  }

It works in part, it does not really let me select the feature before enabling the layer, but after I enable that problem comes in, I select a feature and when I select another it keeps the previous selected and also when I select where there is no selection It does not remove the selection.

So I tried to do this below to correct:

mymap.on('click', function (e) {
  if (!mymap.hasLayer(lote_ocupa)) {
      return;
    }
    else if(identifiedFeature){
      mymap.removeLayer(identifiedFeature);

    }
    lote_ocupa.identify().on(mymap).at(e.latlng).run(function(error, featureCollection){
      identifiedFeature = new L.GeoJSON(featureCollection.features[0], {
        style: function(){
          return {
            color: '#5C7DB8',
            weight: 2
          };
        }
      }).addTo(mymap);          
    });
  });

now works fine !! thank you so much Stephen.

krekto
  • 123
  • 4