bluehalo / ngx-leaflet-draw

MIT License
88 stars 29 forks source link

Delete last point with deleteLastVertex(), limit polyline to two vertexes #60

Closed RobDaPraia closed 5 years ago

RobDaPraia commented 5 years ago

I want to limit the drawing/editing of a polyline to only two points, to get a line instead of a polyline, because I need to make calculations based on the start and end point of a single line that the user draws

I tried to remove the last point from a polyline after the on created event, but was not able to do, the typescript does not find the deleteLastVertex() using the following script.

 onMapReady(map: Map) {
     this._map.on(
         L.Draw.Event.CREATED,
         function(e) {
             if ((e as L.DrawEvents.Created).layerType === 'polyline') {

                const line = this.polylineToLine((e as L.DrawEvents.Created).layer as L.Polyline);

                line.deleteLastVertex();
             }
         }
      );
   }

When I use a different approach, like editing the latlngs of the line, it works more or less, because when you want to edit the line, you see all the original vertexes, see screenshot.

 polylineToLine(line: L.Polyline): L.Polyline {
      //line.editing.enable();
     // line.deleteLastVertex();

      let latlngs = line.getLatLngs() as LatLng[];
      if (latlngs.length > 2) {
         latlngs = latlngs.slice(0, 2);
         line.setLatLngs(latlngs);
      }

      // line.editing
      return line;
   }

leaflet

Do you have an idea/suggestion?

reblace commented 5 years ago

I'm not sure how you'd add this behavior. It sounds more like a question for the leaflet.draw plugin itself. Or, you could ask on Stack Overflow.

reblace commented 5 years ago

I'm going to close this as it's not specifically an issue/question related to this plugin. Rather, it's specific to leaflet.draw, which can be found here: https://github.com/Leaflet/Leaflet.draw

RobDaPraia commented 5 years ago

ok, thanks for the feedback