I want to draw a path with no more then 2 points. On the second point I want the path to be finished.

I'm using the OpenLayers DrawFeature, I set a callbacks for "point" where I count the created points and if the points are two I'm finishing the sketch with finishSketch() method. The problem is that when finishSketch() method calls the finishGeometry() method which removes the last added point from the path which cause the following error to show

"Uncaught TypeError: Cannot read property 'geometry' of null "

My question is the any legit way for finishing a path programmaticaly or this is a bug in the DrawFeature control.

2 Answers 2


It's not a bug in Draw Feature, that's just how 'sketch' works. As you should have noticed, when you drawing line using DrawFeature control, the last point will goes where you move the mouse, and when you finished drawing, it will be removed and the rest of the points forms the result line. That means you must have at least 3 points to finish a line (including the one move with you mouse).

I think you want a tool that finish the line when placing the second point and no more sketch would be shown. In that case, don't call the finishSketch() in 'point' callback, call drawFeature instead. I would imagine the callback function would look like this:

 * pointGeometry: the point you just added
 * lineGeometry: the current line sketch
point: function(pointGeometry, lineGeometry) {
    var numPoints = lineGeometry.getVertices().length;
    if (numPoints == 2) {
  • Can you write a full example including the drawFeature control please? Commented Oct 25, 2014 at 22:10

Simply use property maxVertices

    // single line drawing control
    _singleLineControl = new OpenLayers.Control.DrawFeature(
            eventListeners: { "featureadded": drawingFinished },
            handlerOptions: {
                maxVertices: 2,
                single: true,
                freehand: false,
  • When using OpenLayers v3+, simply use maxPoints in the Draw interaction.
    – juminet
    Commented Feb 8 at 15:00

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.