Matze W
Matze W

Reputation: 43

How get coordinates from polygons inside a Layer

I have a LayerGroup

var mapLayer = new L.layerGroup();

This is how I add several polygons to this LayerGroup:

var buffered = turf.buffer(polyline, path_alarmweight, 'meters');
bufferedPolygon = L.geoJson(buffered, bufferedOptions).addTo(mapLayer);`

How can I get the coordinates from a single or several polygons which I added to the mapLayer?

Upvotes: 2

Views: 439

Answers (1)

Bulva
Bulva

Reputation: 1248

First of all you need to get all layers from LayerGroup, use getLayers() function, in documentation. You will get layers in LayerGroup:

var arrayOfLayers = mapLayer.getLayers();

Then you can iterate over arrayOfLayers and for every layer you can get coordinates of polygon with getLatLngs() function. See the reference:

for(var i=0; i < arrayOfLayers.length; i++) {
// first get array of coordinates
var arrayOfPoints = arrayOfLayers[i].getLatLngs();

    //then iterate over coordinates
    for(var j=0; j < arrayOfPoints.length; j++) {
        console.log(arrayOfPoints[j]);
    }
}  

Upvotes: 1

Related Questions