I am trying to save the polygon into database and later retrieve it back. I am trying the code snippet below.
map.on('draw:created', function (e) {
var type = e.layerType,
layer = e.layer;
if (type === 'polyline') {
// structure the geojson object
var geojson = {};
geojson['type'] = 'Feature';
geojson['geometry'] = {};
geojson['geometry']['type'] = "Polyline";
// export the coordinates from the layer
coordinates = [];
latlngs = layer.getLatLngs();
for (var i = 0; i < latlngs.length; i++) {
coordinates.push([latlngs[i].lng, latlngs[i].lat])
}
// push the coordinates to the json geometry
geojson['geometry']['coordinates'] = [coordinates];
// Finally, show the poly as a geojson object in the console
console.log(JSON.stringify(geojson));
}
else if (type === 'polygon') {
drawnItems.addLayer(layer);
layer.bindPopup('A popup!');
// structure the geojson object
var geojson = {};
geojson['type'] = 'Feature';
geojson['geometry'] = {};
geojson['geometry']['type'] = "Polygon";
// export the coordinates from the layer
coordinates = [];
latlngs = layer.getLatLngs();
for (var i = 0; i < latlngs.length; i++) {
coordinates.push([latlngs[i].lng, latlngs[i].lat])
}
// push the coordinates to the json geometry
geojson['geometry']['coordinates'] = [coordinates];
layer.openPopup();
// Finally, show the poly as a geojson object in the console
console.log(JSON.stringify(geojson));
// console.log(down);
}
The output is always {"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[null,null]]]}}