0

I have an Openlayers 4 polygon and line draw interaction. There are also some construction tools to add vertices from a dialog. These vertices are inserted using the geometryFunction on the draw interaction. The function modifies the coordinates and reapply them to the geometry:

Gui.Draw.Control.prototype._lineDrawGeometryFunction = function (coords,geom){
    if (!geom) {
        geom = new ol.geom.LineString(null);
    }
    if (this._coordinateForAddition != null)
    {
        var popped = coords.pop();
        var popped2 = coords.pop();
        coords.push(popped2);
        if (!(popped2[0] === this._coordinateForAddition[0] && popped2[1] === this._coordinateForAddition[1]))
            coords.push(this._coordinateForAddition);
        coords.push(popped);
        this._coordinateForAddition = null;
    }
    geom.setCoordinates(coords);
    return geom;
};

When drawing using the mouse cursor the sketch geometry is updated on the fly. But when constructed vertices are added the sketch is not updated until the cursor is moved in the map.

How do I force a refresh of the sketch geometry? Or is there a better approach to do this?

Note:

When calling ol.interaction.Draw#removeLastPoint() programatically the sketch is autoupdated. It uses the internal function

this.updateSketchFeatures_()

, but that is not available from the API. Is there a way to trigger this?

What I did try:

I have attempted the following - all without success:

    this.olSketchFeature.set("dummy", createGuid());
    this.olSketchFeature.setStyle(this.olSketchFeature.getStyle());
    this.olSketchFeature.getGeometry().setCoordinates(this.olSketchFeature.getGeometry().getCoordinates());
    this.olSketchFeature.changed();
    this.olSketchFeature.getGeometry().changed();

this.olSketchFeature is cached from the "drawstart" event.

I have also attempted to modify the olSketchFeature geometry, but the changes are removed as soon as the mouse cursor is moved.

1 Answer 1

0

At last i found a way to trigger a refresh of the sketchFeature. By calling handleEvent the refresh is triggered. It can be triggered with a constructed mouse move event like this:

theDrawInteraction.handleEvent({
    type: "pointermove",
    map: this.owner.olMap,
    originalEvent: new Event("mousemove"),
    dragging: false,
    coordinate: theNewCoordinate
});

combined with providing a GeometryFunction to the ol.interaction.Draw it worked.

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.