[GIS] How to store a drawn feature in OpenLayers

geojsonopenlayers

I have found a draw tool in OpenLayers-3 which allows us to draw on a map (see http://openlayers.org/en/v3.2.0/examples/draw-features.html). Is there an API in openlayers to store the drawn feature in geojson format?

Best Answer

Further to the answer above, this is how I'm grabbing the draw features before storing them in an indexed db for offline storage:

// declare vars to populate later
var routetitle, routeDescription, routeFeatures, 
routeScreenshot, routePNG, tags;



// THIS IS THE IMPORTANT BIT
var allFeatures = drawLayer.getSource().getFeatures();
var format = new ol.format.GeoJSON();
var routeFeatures = format.writeFeatures(allFeatures);

If you then console log(routeFeatures it should give you a JSON Feature Collection