无法删除 OpenLayers 3 中的功能
Unable to remove feature in OpenLayers 3
我已经阅读了 Whosebug 上的许多主题,但其中 none 有帮助。所以,这就是我尝试做的事情:
features.forEach(function(feature){
source.removeFeature(feature);
console.log("removed");
console.log(feature);
});
因此,当我只选择了一项功能时,我在控制台中看到了这些消息:
removed Controller.js:525:8
Object { disposed_: false, onDisposeCallbacks_: undefined, ...}
就我在控制台中看到的内容而言,一切看起来都不错。但问题是地图上没有移除地图项。
编辑
现在更有趣了。如果我使用 getArray 将要素转换为数组并执行此操作:
for(var i=0,len=features.length;i<len;i++){
var feature = features[i];
source.removeFeature(feature);
}
source.clear();
当我有很多特征而只选择了一个特征时,在这种情况下,只保留这个选定的特征,其余所有特征都被删除。这到底是怎么回事??
这个问题困扰我很久了,一直想不通。事实证明,这似乎是 OpenLayers 中的刷新问题。我发现让图层刷新的方法是让它不可见然后再可见。
这是我用来解决问题的代码(在 AngularJS 中):
vectorLayer.getSource().removeFeature(feature);
$timeout(function() {
vectorLayer.setVisible(false);
vectorLayer.setVisible(true);
}, 10);
如果您不使用 Angular,只需使用以下内容:
vectorLayer.getSource().removeFeature(feature);
vectorLayer.setVisible(false);
vectorLayer.setVisible(true);
我遇到了类似的问题。对 setVisible 的重复调用对我不起作用。
事实证明,如果您从图层中删除 selected 功能,它会被删除,但它仍然可见。它不会被删除 "Visually" 直到你 select 其他东西。
我做了什么:
// collection that will contain all selected features
var selectFeatures = new ol.Collection();
// setting up the select interaction
var selectInteraction = new ol.interaction.Select({
features: selectFeatures
});
// custom event to clear all selections
selectInteraction.on('clearSelections', function() {
selectedFeatures.clear();
});
removeSomeFeaturesFromLayer() {
// dispatch the event first
selectInteraction.dispatchEvent({
type: 'clearSelections'
});
/*
* NOW LOOP THROUGH THE FEATURES IN THE LAYER
* AND REMOVE WHATEVER ONES YOU WANT
*/
}
希望对您有所帮助。
我已经阅读了 Whosebug 上的许多主题,但其中 none 有帮助。所以,这就是我尝试做的事情:
features.forEach(function(feature){
source.removeFeature(feature);
console.log("removed");
console.log(feature);
});
因此,当我只选择了一项功能时,我在控制台中看到了这些消息:
removed Controller.js:525:8
Object { disposed_: false, onDisposeCallbacks_: undefined, ...}
就我在控制台中看到的内容而言,一切看起来都不错。但问题是地图上没有移除地图项。
编辑
现在更有趣了。如果我使用 getArray 将要素转换为数组并执行此操作:
for(var i=0,len=features.length;i<len;i++){
var feature = features[i];
source.removeFeature(feature);
}
source.clear();
当我有很多特征而只选择了一个特征时,在这种情况下,只保留这个选定的特征,其余所有特征都被删除。这到底是怎么回事??
这个问题困扰我很久了,一直想不通。事实证明,这似乎是 OpenLayers 中的刷新问题。我发现让图层刷新的方法是让它不可见然后再可见。
这是我用来解决问题的代码(在 AngularJS 中):
vectorLayer.getSource().removeFeature(feature);
$timeout(function() {
vectorLayer.setVisible(false);
vectorLayer.setVisible(true);
}, 10);
如果您不使用 Angular,只需使用以下内容:
vectorLayer.getSource().removeFeature(feature);
vectorLayer.setVisible(false);
vectorLayer.setVisible(true);
我遇到了类似的问题。对 setVisible 的重复调用对我不起作用。
事实证明,如果您从图层中删除 selected 功能,它会被删除,但它仍然可见。它不会被删除 "Visually" 直到你 select 其他东西。
我做了什么:
// collection that will contain all selected features
var selectFeatures = new ol.Collection();
// setting up the select interaction
var selectInteraction = new ol.interaction.Select({
features: selectFeatures
});
// custom event to clear all selections
selectInteraction.on('clearSelections', function() {
selectedFeatures.clear();
});
removeSomeFeaturesFromLayer() {
// dispatch the event first
selectInteraction.dispatchEvent({
type: 'clearSelections'
});
/*
* NOW LOOP THROUGH THE FEATURES IN THE LAYER
* AND REMOVE WHATEVER ONES YOU WANT
*/
}
希望对您有所帮助。