1

我有一个 OpenLayers 控件来在我的地图上画一条线。这很好用。现在我添加了一个显示线条坐标的表单。用户应该能够在那里编辑坐标,并在提交表单时更新该行。

问题是我最终得到了两条可见的线。经过一些调试后,我发现,虽然我为 DrawFeature 指定了要使用的层,但处理程序会创建一个名为“OpenLayers.Handler.Path”的新层。所以我用鼠标画的所有东西都画在上面,而通过提交表单创建的线画在“我的图层”上。

我有以下代码:

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]);

那么为什么处理程序创建一个新层而不使用指定的层呢?

4

1 回答 1

1

DrawFeature 控件使用指定的图层,而 OpenLayers.Handler.Path 每次激活时都会创建一个新图层。

这个新层被注释为“临时绘图层”。

您可以尝试覆盖 OpenLayers.Handler.Path 的 activate 和 deactivate 方法,强制它使用指定的图层,但可能会出现一些意想不到的问题。 https://github.com/openlayers/ol2/blob/master/lib/OpenLayers/Handler/Point.js#L156(Handler.Path扩展 Handler.Point)

所以我会尝试用表格来解决问题。您没有发布“表单代码”,所以我不能更具体,但您可以尝试更新两个功能(在两个图层上),或者您可以尝试在表单编辑期间停用绘图控件/处理程序。

于 2016-08-24T07:09:40.000 回答