Exporting features as GeoJSON
In the earlier recipes, we saw how to read in data from different formats, such as GML and KML, but what about exporting data from a map? A user of your mapping application may want to get a copy of the vector layer data to use it somewhere else. Or perhaps, you're saving the state of a map and need to send the layer content to a server for persistent storage so that the same geometry can be retrieved later.
In this recipe, we will export the contents of a vector layer to the GeoJSON format. The source code can be found in ch03/ch03-export-geojson
. Here's what we'll end up with:
How to do it…
We are going to enable the use of an export
button that will output the vector layer features as GeoJSON in a text box. Follow these steps to achieve this goal:
- Create the HTML file with OpenLayers dependencies. In particular, create the following:
<div id="js-map"></div> <div> <h1>Exporting GeoJSON</h1> <...