A
A
Alexander2019-09-27 21:00:38
JavaScript
Alexander, 2019-09-27 21:00:38

How to store polygon coordinates in leaflet?

How to get the coordinates of created polygons in a format like [-145.59082, 81.56997],[-149.80957, 81.4988],[-150.60059, 82.46607],[-146.25, 82.50629]?
or convert from format like "M253 207L450 290L684 178z"

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2019-09-27
@grek_m

polygons are created as objects and can be exported to GeoJSON

var polygon = L.polygon([
  [51.509, -0.08],
  [51.503, -0.06],
  [51.51, -0.047]
]).addTo(mymap);
JSON.stringify(polygon.toGeoJSON().geometry.coordinates[0]) 
""

You can unload all vector objects in general - like this:
var a = [];
mymap.eachLayer(l=>'toGeoJSON' in l && a.push(l.toGeoJSON()));
var result = {"type":"FeatureCollection","features":a};
console.log(JSON.stringify(result,"\n",4))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question