TurtIeSocks / react-leaflet-geoman

React wrapper for the leaflet-geoman plugin
MIT License
19 stars 7 forks source link

Enable draw mode programmatically #12

Closed stiang closed 7 months ago

stiang commented 7 months ago

It feels like I’m missing something obvious, but I can’t figure out how to do stuff like this:

// enable polygon Draw Mode
map.pm.enableDraw("Polygon", {
  snappable: true,
  snapDistance: 20,
});

I believe this would let me initialise the polygon draw mode from a button that I control (separate from the geoman UI), but I can’t figure out how to access map.pm so that I can call the enableDraw method.

I have used the wrapper from your example, and everything works perfectly, but the ref on the FeatureGroup doesn’t appear to give me access to map.pm.enableDraw, at least not in the ways I have tried (things like ref.current.pm.enableDraw and variations). Is there a way to do this?

My goal is to be able to have a couple of buttons on a UI that I have overlaid on the map, with labels such as "Create a polygon" and "Create a circle" to get the user started, which would have the same effect as pressing the corresponding icons in the Geoman toolbar.

stiang commented 7 months ago

I was able to solve this by adding a ref on the MapContainer and then passing it down to the GeomanWrapper component. Apologies for the noise, closing.