简体   繁体   English

在Openlayer3中仅允许一个绘图功能

[英]Allow only one draw feature in Openlayer3

I'm using ol.interaction.Draw to draw points on my map. 我正在使用ol.interaction.Draw在地图上绘制点。 I would like to enable the user to draw one unique point each time he/she clicks on the "draw" icon. 我希望用户每次单击“绘制”图标时都能绘制一个唯一的点。 Any idea on how to do that? 关于如何做到这一点的任何想法?

Here is the code for the interaction: 这是交互的代码:

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

Thanks! 谢谢!

I had the same problem but used a different approach to solve it. 我遇到了同样的问题,但是使用了不同的方法来解决它。 I didn't use an interaction but caught the the click event in the map. 我没有使用交互,但是在地图中捕获了click事件。

var pinpointFeature = new ol.Feature();         
var pinpointOverlay = new ol.FeatureOverlay({
    features: [pinpointFeature]
});         

map.on('click', function(event) {
    pinpointFeature.setGeometry(new ol.geom.Point(event.coordinate));
    //do something with your feature if needed
});

You could possibly remove the interaction from the map on the drawend event. 您可以在drawend事件上从地图上删除交互。

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

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

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