Saturday, 29 October 2016

javascript - leaflet count by featuretypes in featuregroup


Is there any way in leaflet to get the number of rectangles in a feature group? I know this code that work: drawnItems.getLayers().length; but it counts all the objects within the drawnItems featuregroup. I only need the a specific type of features, for example rectangle



Answer




Assuming that you are using instances of L.Rectangle, you can use functional programming like so:


drawnItems.getLayers().filter( function(l) {return l instanceof L.Rectangle} ).length;

The filter method of Array will return another array, containing only the elements which make the callback function return true; in this case, it will return an array containing only those layers which are instances of L.Rectangle.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...