Search code examples
openlayers-3

Open Layers 3 - Initialize Draw Interaction with first node on LineString


I have a map where a user can choose some object/feature on the map and draw a line to another object/feature. When the user selects the feature i would like do add a draw interaction and already set the first point to the selected feature without the user having to click again on the map.

Here is a fiddle: Sample

The commented code below should be executed programmatically without user interaction, after pressing the draw button

    geometryFunction: function (c, g) {
        if (goog.isDef(g)) {
            g.setCoordinates(c);
        } else {
            // DO THIS AUTOMATICALLY ON PRESSING DRAW
            // TO INITIALIZE AND START THE DRAWING PROCESS
            c[0][0] = 1174072.754460305;
            c[0][1] = 332653.94709708635;
            g = new ol.geom.LineString(c);
        }
        ...
    }

The current behaviour is that you click on the Draw button and can click on the map to start drawing (but i overwrite the first node with my desired starting location -- in this example near central africa)

Is it possible to click on Draw and the first node is already programmatically set, without having to click on map first?


Solution

  • It is not currently possible to do manually append points to the OpenLayers 3 ol.interaction.Draw, but it would make sense to be able to support it (in my mind). It would be "as-if" the user had clicked.

    You should ask the OL3-dev mailing this about adding such a feature to see what they think about it. If they agree and you're willing to work on this, you could provide a pull request. See: https://groups.google.com/forum/#!forum/ol3-dev