[英]Allow only one draw feature in Openlayer3
我正在使用ol.interaction.Draw在地圖上繪制點。 我希望用戶每次單擊“繪制”圖標時都能繪制一個唯一的點。 關於如何做到這一點的任何想法?
這是交互的代碼:
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);
}
謝謝!
我遇到了同樣的問題,但是使用了不同的方法來解決它。 我沒有使用交互,但是在地圖中捕獲了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
});
您可以在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.