Search code examples
openlayers

How to get GeoJSON from Openlayers extent or extentinteraction?


Currently I'm using the ExtentInteraction provided by Openlayers and I'm able to use the methods provided by extent to get coordinates. I can even get bottom right/left and top right/left, but I was curious if there's a way simply call a method and obtain the GeoJson output for the extentinteraction?


Solution

  • If you want the interaction's extent as a polygon feature in a GeoJSON string

    var geojson = new ol.format.GeoJSON().writeFeatures([new ol.Feature(ol.geom.Polygon.fromExtent(interaction.getExtent()))]);