Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/409.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Openlayers 3-绘制后修改多边形_Javascript_Openlayers 3 - Fatal编程技术网

Javascript Openlayers 3-绘制后修改多边形

Javascript Openlayers 3-绘制后修改多边形,javascript,openlayers-3,Javascript,Openlayers 3,我试图在使用ol.interaction.Draw绘制多边形后使其可编辑。当我实例化ol.interaction.Modify时,我得到一个“b.attachEvent不是函数”错误这是代码: drawPolygon.on("drawend",function(p) { setTimeout(function() { modifyPoligon = new ol.interaction.Modify({ features: vectorSo

我试图在使用ol.interaction.Draw绘制多边形后使其可编辑。
当我实例化ol.interaction.Modify时,我得到一个“b.attachEvent不是函数”错误
这是代码:

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

我还使用了超时,因为在抽屉调用中,功能仍然不在层中,有没有更好的方法在绘制功能后在层上获得回调?

不确定是否适合您的情况,但这里有一个类似的方法:


它可以简单到:

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

你不需要实例化
ol.interaction.Modify
draund
中,只需将它放在你的
ol.interaction.Draw
之后。如果我没有传递羽毛集合,我会得到一个“forEach”错误。你能提供一个示例代码吗?我认为相反更好,修改一下你的代码,我们一起调试。@JonatasWalker别忘了提供一些反馈!我注意到一件奇怪的事情:在modifyInteraction中,当删除一个顶点(通过单击它)时,geojson是通过
var geojson=formatGeoJSON.writeFeature(featureClone)获得的仍然与删除顶点之前的早期版本相同(请参见console)。这是一个错误吗?…它总是落后一步:如果你画一个有5个顶点的多边形,你会得到一个有6个坐标的GeoJSON(很好,第一个坐标等于最后一个坐标)。如果删除一个顶点,GeoJSON将保持不变。如果删除另一个,GeoJSON包含5个坐标。在ol.interaction.Modify中删除顶点时不会更新混淆特征:
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);