Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Leaflet-Draw: Get polygon latLng in 'draw:editvertex' event

When a draw:editvertex event fires, how can I get information about the polygon which triggered it?

this.map.on('draw:editvertex', function (e) { debugger;
    var layers = e.layers;
    // I want to get current polygon latLng here
}.bind(this));
like image 698
Kalashir Avatar asked Jul 21 '16 12:07

Kalashir


2 Answers

This approach works for me (but doesn't feel like best practice) –

In my draw:editvertex handler I loop through the target._layers and look for the edited property:

map.on('draw:editvertex', function(e) {
    for (thisLayer in e.target._layers) {
        if (e.target._layers.hasOwnProperty(thisLayer)) {
            if (e.target._layers[thisLayer].hasOwnProperty("edited")) {
                console.log("we think we found the polygon?");
                console.log(e.target._layers[thisLayer]);

                // the updated Polygon array points are here:
                newPolyLatLngArray = e.target._layers[thisLayer].editing.latlngs[0];
            }
        }
    };
});

...like I said, this doesn't feel Awesome, but it is working for me so far.

like image 144
AKA Avatar answered Nov 05 '22 16:11

AKA


There are not only layers in e, but also the target layer poly can be approached easily.

map.on('draw:editvertex', function (e) { 
  var poly = e.poly;
  var latlngs = poly.getLatLngs(); // here the polygon latlngs
});
like image 1
Jayson Chiang Avatar answered Nov 05 '22 17:11

Jayson Chiang