简体   繁体   English

Openlayers 3-绘制后修改多边形

[英]Openlayers 3 - Modifying a polygon after draw

I am trying to make a polygon editable after it is drawn with ol.interaction.Draw. 我正在尝试使用ol.interaction.Draw绘制多边形后使其可编辑。
When I instantiate ol.interaction.Modify I get a "b.attachEvent is not a function" error. 实例化ol.interaction.Modify时,出现“ b.attachEvent不是函数”错误。
This is the code: 这是代码:

drawPolygon.on("drawend",function(p)
{
    setTimeout(function()
    {
        modifyPoligon = new ol.interaction.Modify({
            features: vectorSource.getFeatures()
        });
    },200);
}

I also use a timeout because in the drawend call the Feature is still not in the layer, is there a better way to get a callback after the feature is drawn and on the layer? 我还使用了超时,因为在drawend调用中Feature仍然不在图层中,是否有更好的方法在绘制Feature之后在图层上获取回调?

Not sure if suits your case but here's a fiddle with a similar approach: https://jsfiddle.net/ko822xjw/ 不确定是否适合您的情况,但这是一种使用类似方法的小提琴: https : //jsfiddle.net/ko822xjw/

// Create a draw interaction and add it to the map:
drawInteraction = new ol.interaction.Draw({ source:vectorSource, type:"Polygon" });
map.addInteraction(drawInteraction);
// set listener on "drawend":
drawInteraction.on('drawend', function(e) {
  // get drawn feature:
  var feature = e.feature;
  // remove draw interaction:
  map.removeInteraction(drawInteraction);
  // Create a select interaction and add it to the map:
  selectInteraction = new ol.interaction.Select();
  map.addInteraction(selectInteraction);
  // select feature:
  selectInteraction.getFeatures().push(feature);
  // do something after drawing (e.g. saving):
  // ...
  // Create a modify interaction and add to the map:
  modifyInteraction = new ol.interaction.Modify({ features: selectInteraction.getFeatures() });
  map.addInteraction(modifyInteraction);
  // set listener on "modifyend":
  modifyInteraction.on('modifyend', function(evt) {
    // get features:
    var collection = evt.features;
    // There's only one feature, so get the first and only one:
    var featureClone = collection.item(0).clone();        
    // do something after modifying (e.g. saving):
    // ...
  });        
});

It could be as simple as: 它可能很简单:

var
    features = new ol.Collection(),
    modify = new ol.interaction.Modify({
        features: features
    }),
    vectorSource = new ol.source.Vector({
        features: features
    }),
    vectorLayer = new ol.layer.Vector({
        source: vectorSource
    }),
    map = new ol.Map({
        layers: [
            new ol.layer.Tile({
                source: new ol.source.MapQuest({layer: 'osm'})
            }),
            vectorLayer
        ],
        target: 'map',
        view: new ol.View({
            center: [0, 0],
            zoom: 2
        })
    }),
    drawPolygon = new ol.interaction.Draw({
        features: features,
        type: 'Polygon'
    })
;
map.addInteraction(modify);
map.addInteraction(drawPolygon);

http://jsfiddle.net/jonataswalker/r2g1atty/ http://jsfiddle.net/jonataswalker/r2g1atty/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM