Openlayers 3:在双击触发drawend后继续绘制初始Line

时间:2017-08-23 10:16:33

标签: draw openlayers

是否可以在' drawend'之后继续从LineString的最后一点绘制。从双击中提出?我在网上找到的每个例子都是从头开始另一次抽奖。

2 个答案:

答案 0 :(得分:1)

您可以将Draw交互与Modify交互相结合。这仍然不允许你继续绘制第一行,但它至少允许你修改现有的并将新的顶点添加到绘制的行。

但是如果你还在混音中添加一个Snap交互,你可以继续你离开的地方,虽然会启动一个新的LineString。之后您需要做的就是将绘制的LineStrings连接到单个LineStrings,或者从所有LineStrings创建MultiLineString。您可以使用http://openlayers.org/en/v4.3.1/examples/draw-and-modify-features.html示例进行测试。

答案 1 :(得分:0)

在这个问题困难了两天后,这是我使用JSTS库(OL3上的库集成的example)所做的:

var vectorSource = new ol.source.Vector();

var map = new ol.Map({
    layers: [
        new ol.layer.Vector({
            source: vectorSource
        })
    ],
    target: document.getElementById('map'),
    view: new ol.View({
        center: ol.proj.fromLonLat([126.979293, 37.528787]),
        zoom: 15
    })
});

var draw = new ol.interaction.Draw({
    source: vectorSource,
    type: 'MultiLineString'
});

var modify = new ol.interaction.Modify({source: vectorSource});
var snap = new ol.interaction.Snap({source: vectorSource});
var select = new ol.interaction.Select({source: vectorSource});
map.addInteraction(draw);
map.addInteraction(modify);
map.addInteraction(snap);

var ol3Parser = new jsts.io.ol3Parser();

//listen to drawend
draw.on('drawend', function (evt) {
    mergeLineString(evt.feature);
});

//listen to modifyend
modify.on('modifyend', function (evt) {
    mergeLineString(evt.features.getArray()[0]);
});

function mergeLineString(currentFeature){
    var features: vectorSource.getFeatures();
    var feature, currentGeo, featureGeo, union, i;
    // loop forward because we have to remove the merged features
    for (i = features.length - 1; i >= 0; --i) {
        feature = features[i];
        // convert data with JSTS
        currentGeo = ol3Parser.read(currentFeature.getGeometry());
        featureGeo = ol3Parser.read(feature.getGeometry());
        // check intersection with another feature already on the vector
        if (currentGeo.intersects(featureGeo)) {
            // create union
            union = currentGeo.union(featureGeo);
            // set the new geometry to the last feature added
            currentFeature.setGeometry(ol3Parser.write(union));
            // remove the feature which was merged
            vectorSource.removeFeature(feature);
        }
    }
}

如果需要,这个代码的任何改进都非常受欢迎,因为我对OL3很新 - 我需要这个功能很多!

/!\警告:使用此代码,请勿在ahocevar建议的同时使用绘图和修改 - 否则您将无法继续使用现有的线串(点击不会听)。您必须绑定两个不同的动作:在绘图按钮上绘制+捕捉,然后在编辑按钮上修改+捕捉。