Reputation: 3566
I want to listen 'bounds_changed' event when the user moves the map, changes the zoom but I don't want it to be fired when my program calls setCenter or setZoom methods. So I tried removing event before setting center and adding it after, again. However, it didn't worked, My event is still being fired.
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();
}
I think the map is creating bounds_changed event after I add the new listener to it, like the event is asynchronised.
Upvotes: 3
Views: 3722
Reputation: 9407
The bounds_changed event is indeed fired asynchronously so, instead of removing the listener you could use a global boolean variable that indicates when to ignore it, for example:
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);
}
Upvotes: 3