0

I am using leaflet map, which can contain polygon(s). I want to check that map has any polygon and if it has then i want to find coordinates of that polygon?

Hunzla Ali
  • 383
  • 2
  • 5
  • 22

1 Answers1

3
map.eachLayer(function(layer){
    if(layer instanceof L.Polygon && !(layer instanceof L.Rectangle) ){
        console.log(layer.getLatLngs());
    }
});
Falke Design
  • 10,635
  • 3
  • 15
  • 30
  • Thanks it worked. Can you tell me one more thing? i can also draw polygon on map. I have removed that by clearLayers() function but it still shows in map layer. How can i remove this from map? – Hunzla Ali Mar 02 '20 at 12:56
  • can you pls add the code where you adding the polygon to the map and how you call clearLayers. Because clearLayers can't called on `map` so you are using a layergroup. – Falke Design Mar 02 '20 at 13:07
  • // Initialise the FeatureGroup to store editable layers this.editableLayers = new L.FeatureGroup(); this.map.addLayer(this.editableLayers); ///add layer while draw this.map.on('draw:created', function(e) { var type = e.layerType, layer = e.layer; if (type === 'marker') { layer.bindPopup('A popup!'); } layer.on("click", function(e) { console.log(" Points: " + e.target.getLatLngs()); }); that.editableLayers.addLayer(layer); }); ///remove function public RemoveDrawPolygon(){ this.editableLayers.clearLayers(); } – Hunzla Ali Mar 02 '20 at 13:12
  • It is working for me ... I think this is because leaflet draw adds already the layer to the map / a group and then you adding it again to another group. And when you remove it, you are removing it from the editableLayers group but not from the leaflet-draw group – Falke Design Mar 02 '20 at 13:28