Leaflet.draw icon indicating copy to clipboard operation
Leaflet.draw copied to clipboard

close shape on polyline tool

Open jasonbaik opened this issue 9 years ago • 4 comments

hi, is there anyone that knows how to invoke "completeShape()" on the polyline tool? currently i check for how many vertexes are created but can't seem to complete the polyline after 1 vertex is drawn:

map.on('draw:drawvertex', function(e) {
      if (Object.keys(e.layers._layers).length > 1) {
        // need to close polyline here
      }
});

jasonbaik avatar Oct 14 '16 16:10 jasonbaik

Not a fantastic answer here, but you can listen to the draw-start event to grab the current (new) shape and save it to a scope before your draw:vertex listener to fire the complete shape on the polyline.

This event really should include the current layer in the callback instead of the group of layers.

ddproxy avatar Nov 05 '16 02:11 ddproxy

Hi Baikon, Did you get any workaround here to invoke "completeShape()" ? I need to close shape after 4 vertex is drawn

brijeshpandey9544 avatar Mar 09 '17 17:03 brijeshpandey9544

Hello @ddproxy, I'm experiencing the same issue. The difference is I want to draw polyline with provided LatLngs using polylineDrawer.addVertex(). When I added the last LatLng, I invoke the polylineDrawer.completeShape(), but it gives me a bug. My polyline is gone. Would you please help me with this? Thanks. Here I attached my code to give much clearer context.

this.mapInstance.polylineDrawer = new L.Draw.Polyline(this.mapInstance.map as unknown as L.DrawMap);
this.mapInstance.polylineDrawer.enable();

const points = res.map(p => L.latLng(p.lat, p.lng));

points.map(p => {
  this.mapInstance.polylineDrawer.addVertex(p); // add vertex point
});

/* Here my line gone if I uncomment this */
// this.mapInstance.polylineDrawer.completeShape();

rhenaldkarrel avatar Nov 09 '22 09:11 rhenaldkarrel

Hello @ddproxy, I'm experiencing the same issue. The difference is I want to draw polyline with provided LatLngs using polylineDrawer.addVertex(). When I added the last LatLng, I invoke the polylineDrawer.completeShape(), but it gives me a bug. My polyline is gone. Would you please help me with this? Thanks. Here I attached my code to give much clearer context.

this.mapInstance.polylineDrawer = new L.Draw.Polyline(this.mapInstance.map as unknown as L.DrawMap);
this.mapInstance.polylineDrawer.enable();

const points = res.map(p => L.latLng(p.lat, p.lng));

points.map(p => {
  this.mapInstance.polylineDrawer.addVertex(p); // add vertex point
});

/* Here my line gone if I uncomment this */
// this.mapInstance.polylineDrawer.completeShape();

Sorry, this issue is already resolved. I invoked the completeShape() method after my draw events listener (e.g. draw:created, etc), and suddenly, this works as expected. Please just inform if anyone finds another case. Thank you.

rhenaldkarrel avatar Nov 10 '22 03:11 rhenaldkarrel