bluehalo / ngx-leaflet-draw

MIT License
88 stars 29 forks source link

instantiate drawing programatically #116

Open cscrum opened 3 years ago

cscrum commented 3 years ago

I am needing to draw a rectangle on the map without having to press a button. I'm having difficulty figuring out how to put the map into drawing mode without the button. I've tried based on examples from leaflet-draw, but nothing seems to be working for me. Is there a way to enable the drawing mode to add a rectangle programatically?

cscrum commented 3 years ago

I have tried this based on other issues here, but I keep getting an error. setRectmode(){ var self = this; var shapeOptions = { stroke: true, color: '#f06eaa', weight: 4, opacity: 0.5, fill: true, fillColor: null, //same as color by default fillOpacity: 0.2, clickable: true, editable: true }; var rectDraw = new (L as any).Draw.Rectangle(self.map, shapeOptions); <<<error here rectDraw.enable(); this.drawnItems.addLayer(rectDraw); }

ERROR TypeError: Cannot read property 'Rectangle' of undefined

  I've also tried changing the line where the error occurs to this
  var rectDraw = new (L as any).Rectangle(self.map, shapeOptions);

  ERROR TypeError: Cannot read property 'lat' of undefined

Any pointers as to what I might be doing wrong? The goal is to put the map into a mode where I can draw a rectangle on the map.