javascript - 在 Openlayers 3 中只允许一个绘图功能

标签 javascript html openlayers-3

我正在使用 ol.interaction.Draw 在我的 map 上绘制点。我想让用户每次点击“绘制”图标时都能绘制一个独特的点。 知道如何做到这一点吗?

这是交互代码:

function addInteraction() {
draw = new ol.interaction.Draw({
  source: sourceComments,
  type: "Point"
});

draw.on('drawend',
  function(evt) {
    // unset sketch
    sketch = null;
    var allFeatures = comments.getSource().getFeatures();
    var format = new ol.format.GeoJSON();
    document.getElementById('geometry').value =   JSON.stringify(format.writeFeatures(allFeatures), null, 4);
  }, this);

map.addInteraction(draw);
}

谢谢!

最佳答案

您可以在 drawend 事件中从 map 中删除交互。

var draw;

function addInteraction() {
    draw = new ol.interaction.Draw({
        source: sourceComments,
        type: "Point"
    });

    draw.on('drawend', function(evt) {
        //... unset sketch
        map.removeInteraction(draw);
    }, this);

    map.addInteraction(draw);
}

关于javascript - 在 Openlayers 3 中只允许一个绘图功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27501026/

相关文章:

javascript - 如何为 JS 重定向添加延迟

npm - 通过 npm/webpack 加载预构建模块的调试版本

javascript - 排序数组和 indexOf() 运行时

javascript - 窗口滚动条触底查询更多数据

javascript - 使用 javascript 移动 div 会导致位置 :relative to position:static 发生意外更改

html - 苹果触摸图标不起作用

javascript - 在 OpenLayers 3 中使用平铺的 Google map

javascript - OpenLayers 3悬停每次只突出显示一个指定的对象

javascript - 如何动态添加 javascript 变量值作为 html 元素的属性?

javascript - 选择框在迭代时不显示