【发布时间】:2015-10-13 22:12:25
【问题描述】:
我在 openlayers 3 (v3.9.0) 中有 Select 和 Draw 交互,我想为其添加一些独特的行为。目前,在绘制feature 后,我必须单击feature 到select。有没有办法完全绕过click 事件并让功能在drawend 上自动选择?
谢谢
【问题讨论】:
-
选择一个正确的答案,不要一直悬而未决。
我在 openlayers 3 (v3.9.0) 中有 Select 和 Draw 交互,我想为其添加一些独特的行为。目前,在绘制feature 后,我必须单击feature 到select。有没有办法完全绕过click 事件并让功能在drawend 上自动选择?
谢谢
【问题讨论】:
您只需在ol.interaction.Select 上调用getFeatures(),然后将新功能添加到此可观察集合中:
selectCtrl = new ol.interaction.Select();
drawCtrl = new ol.interaction.Draw();
drawCtrl.on("drawend",function(e){
selectCtrl.getFeatures();
features.push(e.feature);
});
【讨论】:
解决了。 ol.interaction.select 在draw.on('drawend',()) 自行解析后触发。诀窍是在添加新功能后强制 select.condition 返回 false。有关详细信息,请参阅我的 jsfiddle 中 selectedFeature.push(evt.feature) 和 var featureadded 的使用。
【讨论】:
如果你和我一样,也想在绘图结束后自动离开绘图模式并返回选择模式(并且选择了特征),你可以这样做:
mySelect = new ol.interaction.Select();
myDraw = new ol.interaction.Draw();
lastDrawnFeature = null;
myDraw.on('drawend',function(e){
lastDrawnFeature = e.feature;
// switch to select interaction
myDraw.setActive(false);
mySelect.setActive(true);
});
mySelect.on('select',function(e){
if (lastDrawnFeature) {
// Actual selection has to be done here,
// otherwise the last point drawn will be selected instead.
mySelect.getFeatures();
features.clear();
features.push(lastDrawnFeature);
lastDrawnFeature = null;
}
});
【讨论】: