1

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

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) {
        this.drawFeature(lineGeometry);
    }
}
1
  • Can you write a full example including the drawFeature control please?
    – camdenl
    Oct 25 '14 at 22:10
1

Simply use property maxVertices

    // single line drawing control
    _singleLineControl = new OpenLayers.Control.DrawFeature(
        _singleLineLayer,
        OpenLayers.Handler.Path,
        {
            eventListeners: { "featureadded": drawingFinished },
            handlerOptions: {
                maxVertices: 2,
                single: true,
                freehand: false,
            }
        });
    viewer.addControl(_singleLineControl); 
    _singleLineControl.deactivate();

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.