In openlayers there are 2 paradigms for drawing features one is to create a "draw interaction" that lets the user draw a feature and the other is to create the geometry and feature yourself--then add it to the layer.

I would like to programmatically start a polygon "interaction" drawing for the user by placing the first point. After which the user will place the remaining points. It therefore must be an "interaction".

This means I must simulate a user click or draw event that the openlayers api accepts as valid.

I've tried..


/*draw being ol.interaction.Draw*/
  • Did you check this? gis.stackexchange.com/questions/160545/… – Ömür Bilgili Sep 25 '17 at 13:38
  • 1
    That question is about programmatic drawing. Not placing points as part of a user draw "interaction". They are very different. I'll try to update the question for clarity. – N-ate Sep 25 '17 at 16:32

In non minified (ol-debug) version you can use function startDrawing_ (doc is here) which accepts event as a parametr. It uses only coordinates from that event, so it could be replaced by a simple object. Problem is I dont know how is this function named in minified version and also that name could change in time. Also it is not a very clean approach...

var draw = new ol.interaction.Draw({features:new ol.Collection([]), type:"Polygon"});

You can use the ol.interaction.Draw.handleEvent method for that. Its documentation says:

... may actually draw or finish the drawing.

See: ol.interaction.Draw API doc.

  • That fires when there is a user "Draw" event in response to a user's input. It does not allow one to programmatically draw the first point in a user draw "interaction". – N-ate Sep 25 '17 at 16:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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