1

I have an OpenLayers control to draw a line on my map. That works fine. Now I added a form where the coordinates of the line are displayed. The user should be able to edit there the coordinates and on submitting the form the line should be updated.

The problem is that I end up with two lines visible. After some debugging I found out that, although I specify for DrawFeature which layer to use, the handler creates a new layer called "OpenLayers.Handler.Path". So everything I draw with the mouse is drawn on that, while the line created by submitting the form is drawn on "My Layer".

I have the following code:

layer = new OpenLayers.Layer.Vector("My Layer");
geoExtMap.map.addLayer(layer);

Control = {
    line: new OpenLayers.Control.DrawFeature(layer,
        OpenLayers.Handler.Path, {
            callbacks: {
                "point": pointHandler,
                "done": doneHandler
            },
            handlerOptions: {
                persist: true,
                maxVertices: 2,
                freehand: false,
                layerOptions: {
                    styleMap: styleMapControls
                }
            }
        })
};

geoExtMap.map.addControl(Control.line);


var points = new Array(
    new OpenLayers.Geometry.Point(x1, y1).transform(EPSG, projectData.crs),
    new OpenLayers.Geometry.Point(x2, y2).transform(EPSG, projectData.crs)
);

var line = new OpenLayers.Geometry.LineString(points);
var lineFeature = new OpenLayers.Feature.Vector(line, null, sketchSymbolizersControls.Line);

layer.removeAllFeatures();
layer.addFeatures([lineFeature]);

So why does the handler creates a new layer and not use the one specified?

ahocevar
  • 5,448
  • 15
  • 29
Matthieu
  • 437
  • 6
  • 16

1 Answers1

1

The DrawFeature control uses the specified layer, while the OpenLayers.Handler.Path creates a new layer everytime is activated.

This new layer is commented as "The temporary drawing layer".

You could try overriding the activate and deactivate method of OpenLayers.Handler.Path, forcing it to use the specified layer, but there could be some unexpected problem. https://github.com/openlayers/ol2/blob/master/lib/OpenLayers/Handler/Point.js#L156 (Handler.Path extends Handler.Point)

So I'd try instead to solve the problem with the form. You didn't post the "form code", so I cannot be more specific, but you could try to update both features (on both layers), or you can try deactivating the draw control/handler during the form edits.

fradal83
  • 2,002
  • 1
  • 10
  • 9
  • 1
    Thanks for the explanation. I already thought something like that was going on, but was not sure. I just solved my problem by toggling twice the button for the control which removes the temporary line but keeps the control active. – Matthieu Aug 24 '16 at 07:47