我想在用户移动地图时监听'bounds_changed‘事件,更改缩放,但我不希望它在我的程序调用setCenter或setZoom方法时被触发。因此,我尝试在设置中心之前删除事件,并在设置中心之后添加它。然而,它并没有起作用,我的事件仍在被触发。
var currentBoundsListener = null;
function addBoundsChangedListener() {
currentBoundsListener = google.maps.event.addListener(map, 'bounds_changed', function () {
// Whatever.
});
}
function setCenter(lat, lng) {
google.maps.event.removeListener(currentBoundsListener);
var geo = new google.maps.LatLng(lat, lng);
map.setCenter(geo);
addBoundsChangedListener();
}
我认为映射是在我添加新的侦听器之后创建bounds_changed事件的,就像事件是异步的一样。
发布于 2012-08-03 15:34:22
bounds_changed事件实际上是异步触发的,因此您可以使用全局布尔变量来指示何时忽略它,而不是删除侦听器,例如:
var ignore = false; // this var is global;
currentBoundsListener = google.maps.event.addListener(map, 'bounds_changed', function () {
if(ignore) {
ignore = false;
return;
}
// Whatever.
});
function setCenter(lat, lng) {
var geo = new google.maps.LatLng(lat, lng);
ignore = true;
map.setCenter(geo);
}
https://stackoverflow.com/questions/11790905
复制相似问题