最近在做项目时发现一个坑,当给图层加了一个交互功能ol.interaction.Select删除要素时,发现图层刷新后被选中的要素没有从地图上删除掉!
震惊ing!!!
最后才发现在清空图层(tool_poiVector.getSource().clear();)前,需要解除交互功能( mapHelper.map.removeInteraction(select);)
function bindSingleClick ( ) {
select = new ol. interaction. Select ( {
layers: [ tool_poiVector]
} ) ;
select. on ( 'select' , function ( e) {
var feature = e. selected[ 0 ] ;
swal ( {
title: "警告" ,
text: '确认要删除要素吗! 此操作不可恢复!' ,
type: "warning" ,
showCancelButton: true ,
confirmButtonColor: "#DD6B55" ,
confirmButtonText: "确认" ,
cancelButtonText: "取消" ,
closeOnConfirm: false ,
showLoaderOnConfirm: true ,
} , function ( ) {
( asmxUrl, JSON . stringify ( { id: feature. getId ( ) } ) , true , function ( e) {
console. log ( e) ;
if ( e. flag) {
map. removeInteraction ( select) ;
tool_poiVector. getSource ( ) . removeFeature ( feature) ;
tool_poiVector. getSource ( ) . clear ( ) ;
swal ( "删除成功!" , '' , "success" ) ;
} else {
swal ( "保存失败!" , "请稍后重试,如有问题请联系开发人员" , "error" ) ;
}
_addFeatureToLayer ( ) ;
} ) ;
} ) ;
} ) ;
map. addInteraction ( select) ;
}