My Leaflet project allows the users to draw shapes (lines, rectangles and polygons). The user can click on the shapes to get their statistics (area, perimeter, etc).
I tried a click event on my FeatureGroup(), where all the shapes I have drawn is added. I am not sure if this is the best approach. Then upon click, a function taking an event is called. The layer type is inferred from the event object.
//Handlers for when drawn shapes are clicked
editableLayers.on('click', onLayerClick);
function onLayerClick(e)
{
let type = e.layerType,
layer = e.layer;
if (type === 'polygon') {
polygons.push(e.layer);
let area = L.GeometryUtil.geodesicArea(layer.getLatLngs()[0]);
console.log("New polygon area: " + area);
}
if (type === 'rectangle') {
rectangles.push(e.layer);
let area = L.GeometryUtil.geodesicArea(layer.getLatLngs()[0]);
console.log("New rectangle area: " + area);
}
}
The type object returns an undefined, and the layer object returns a bunch of parameters making no reference to the shape type. Because of that, I cannot infer the shape type and perform the correct calculations to get their stats.