Search code examples
openlayers

Is there a property to limit Openlayers extent interaction to designated number of geocells or degrees?


In this instance there really isn't much code to share, but here is the document for Openlayers extent interaction:

Openlayers

I'm using the extent interaction to allow users to draw a bounding box and I'd like to limit the size of the extent that can be drawn by specifying a number of geocells or degrees. I'm hoping to be able to set the limits before the users draws the extent, but don't see a straight forward way to accomplish this within the OL documents. As of now, the users can simply create an extent of any size then I craft geojson like so:

new GeoJSON().writeFeaturesObject([new Feature(fromExtent(extentCopy))],
                { dataProjection: 'EPSG:4326', featureProjection:        map.getView().getProjection()});

Any advice would be appreciated.


Solution

  • You have to use a Draw interaction to draw a rect: https://openlayers.org/en/latest/examples/draw-shapes.html

    Then listen to the drawstart event of the Draw interaction to get the drawn object and attach a listerner on its geometry to get its changes: event.feature.getGeometry ().on ('change', testgeom)

    You can test the geometry changes in the testgeom function to prevent changes.