How to save a completed polygon points leaflet.draw to mysql table

user3703511 picture user3703511 · Jun 3, 2014 · Viewed 26.5k times · Source

I would like to use leaflet.draw to create outlines of regions. I have managed to get this working ok: https://www.mapbox.com/mapbox.js/example/v1.0.0/leaflet-draw/

Now I'd like to save the data for each polygon to a mysql table. Am a little stuck on how I would go about exporting the data and the format I should be doing it in.

If possible I'd like to pull the data back into a mapbox/leaflet map in the future so guess something like geojson would be good.

Answer

Michael Evans picture Michael Evans · Jun 3, 2014

So you could use draw:created to capture the layer, convert it to geojson then stringify it to save in your database. I've only done this once and it was dirty but worked.

map.on('draw:created', function (e) {
  var type = e.layerType;
  var layer = e.layer;

  var shape = layer.toGeoJSON()
  var shape_for_db = JSON.stringify(shape);
});