In my React app, I'm using Geoman on a Leaflet map with several Geoman tools. Currently, I have an event listener that verify which tool is used and chose the right function to fire when the user has finished with the tool :
useEffect(() => {
map.on("pm:create", ({ shape, layer }) => {
if (mapMode === MapMode.SPLIT_SPACES) {
handlingSplit(shape, layer);
} else {
handlingCreate(shape, layer);
}
setMapMode(MapMode.NONE);
});
return (): void => {
if (map.hasEventListeners("pm:create")) {
map.removeEventListener("pm:create");
}
};
}, [map, setMapMode, handlingCreate, mapMode]);
I'd like to add a button to trigger the handlingSplit()
function instead of clicking on the points on the map. The problem is, this function needs both shape
and layer
provided by the pm:create
event. Is there any way to get those data ?
You will always need a reference to a layer, so you need to store the data of the last created layer and then if you click on a button get this data from the variable.
I can't say you how to do this in react but in vanilla it looks like this:
A custom control can be created like this:
https://jsfiddle.net/qx07eLn2/