0

I have a problem with binding a removal function to every layer (polygon) I load from kml file (even just coordinates stored from database). It works only for the first click - first layer I click on. After that it does not work for other layers, seems like it is disabled after first click or something.

When I put there bindPopup function, it works for every layer, and popup shows up, or console.log(this) shows object details for every clicked layer. So I don't get why removeLayer does not work fore each layer as well.

I guess I am missing some knowledge here with javascript, can you please advise?

(KMLfile is a file extension made by Google to extract areas/locations/etc. from google earth app)

Code for loading KML file:

var filePath = 'path to my kml file';
var customLayer= 'colors, line weight, etc.';

var runLayer = omnivore.kml(filePath, null, customLayer).on('ready', function() {
            map.fitBounds(runLayer.getBounds());
            runLayer.eachLayer(function(layer) {

                //layer.bindPopup(layer.feature.properties.name+" / "+this);

                layer.on('click', function() {
                    map.removeLayer(layer); // There's the issue, itworks only on first click!
                });

            });
         }).addTo(loadedFeatureGroup);//adding polygons to predefined group
Jaroslav
  • 1
  • 1

2 Answers2

0

You should use the event object in your callback

layer.on('click', function(e) {
    map.removeLayer(e.target); 
});

Click on the layers in this example: http://plnkr.co/edit/iY6jqvzsnnX7lMeGuuil?p=preview

YaFred
  • 9,698
  • 3
  • 28
  • 40
  • Did not work for me ... the difference is that I load lat/lngs from database and create polygons in a function and add them to existing feature layer, so maybye I should start from there, but I have no idea if it will work ... – Jaroslav Mar 29 '16 at 13:26
0

So the problem was that L_PREFER_CANVAS was set to true. you can read more here:

https://github.com/Norkart/Leaflet-MiniMap/issues/35

Jaroslav
  • 1
  • 1