I'd like to enable a draw function in an Openlayers 3 application for drawing a rectangle - up to a certain size (in the defined projected coordinate system, in square meters). For this ol.interaction.draw instance the draw.createBox geometryFunction would be used and the 'Circle' type, as per


the geometryFunction is where to "modify the geometry during drawing" as per a prior thread:

Undoing last point when drawing linestring in OpenLayers 3? )

but for drawing a rectangle the predefined createBox geometryFunction is used and not a custom one that might perhaps continually check the size of the rectangle. Or is this not the place to put in a check of the current size of the rectangle being drawn? Is what I wish realistic?

1 Answer 1


I think it is possible, by that approach, but you would need to specify custom named function which will do that. Either you can write it on your own (involving some math propably), or you could edit function drawBox or you can write function that would call the function drawBox, check its geometry or area respectively and if it passes your limitaitons you will return it otherwise you will return the biggest box possible for example (that is really up to you). Something like this:

function createLimitedBox(coordinates, opt_geometry){
   let box = createBox();//calling the ol function
   if(box.getArea() > maxVal){//you need to find how to get area of ol.geometry
       return maxBox;/*you need to create this box based on cursor position, 
                      initial point and your limitaions*/
   } else{
       return box;
draw = new Draw({
        source: source,
        type: value,
        geometryFunction: geometryFunction

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.