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.
I would leverage the
instanceof
operator for this task, e.g.:Note that due to how inheritance works, any instance of
L.Rectangle
is also an instance ofL.Polygon
, andL.Polyline
andL.Path
- that's why the code should check first for the most specific subclasses.Remember to check out the Leaflet reference, specifically for the bits which tell you about the inheritance tree, e.g.:
The Leaflet class diagram is also useful for understanding the inheritance tree.
The root cause is that
event.layerType
is a special property added by Leaflet.draw plugin, only when a new feature has been created by a user.When you attach your click event listener on Leaflet standard Feature Group, the click event object does not have such layerType property, as you can check on the docs.
As for a solution, see IvanSanchez's answer.