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

别来无恙 提交于 2021-01-28 18:08:45

问题


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?


回答1:


It is not 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




回答2:


If you don't mind using a private method in OL you can do this to achieve what you want.

var event = $.Event('click'); //create a click event in your draw method using JQuery 
event.coordinate = [1174072.754460305,332653.94709708635];// set your starting coordinate
draw_interaction.startDrawing_(event);// tell your interaction to start drawing


来源:https://stackoverflow.com/questions/35944733/open-layers-3-initialize-draw-interaction-with-first-node-on-linestring

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!