如何临时禁用viewchangeend
事件,使用setView
方法更改视图并重新启用viewchangeend
事件?
这是我现在正在做的更轻松的版本:
var mapEventsObjects = [];
/* Remove map events */
for ( var i = 0; i < mapEventsObjects.length; i++ ) {
Microsoft.Maps.Events.removeHandler( mapEventsObjects[i] );
}
/* Change the view */
map.setView({
"zoom": zoom,
"center": new Microsoft.Maps.Location( lat, long )
});
/* Add map events back */
var mapViewChangeEnd = Microsoft.Maps.Events.addHandler(map, "viewchangeend", function (e) {
console.log("viewChangeEnd Fired: ", e );
});
mapEventsObjects.push( mapViewChangeEnd );
我希望在控制台中看不到 viewChangeEnd Fired ,但它不会出现一次而是两次。
知道为什么它不起作用?
答案 0 :(得分:0)
您可以创建一个简单布尔值的gobal变量。如果要忽略viewchangeend事件,请将变量设置为true。然后在viewchangeend事件的事件处理程序中,您可以进行简单检查,如果此变量为true,则将变量设置为false并保留该函数。以下是代码的修改版本:
var mapEventsObjects = [], skipViewChangeEnd = false;
//Add view change end event to map.
var mapViewChangeEnd = Microsoft.Maps.Events.addHandler(map, "viewchangeend", function (e) {
//Check to see if you want to skip this event once.
if(skipViewChangeEnd){
skipViewChangeEnd = false;
console.log("viewChangeEnd skipped");
}else{
//Your logic for handling this event
console.log("viewChangeEnd Fired: ", e );
}
});
mapEventsObjects.push( mapViewChangeEnd );
skipViewChangeEnd = true;
/* Change the view */
map.setView({
"zoom": zoom,
"center": new Microsoft.Maps.Location( lat, long )
});