Benbob
Benbob

Reputation: 14264

Google maps get_bounds event triggered multiple times

I'm using V3 of the Google maps javascript api to bring in more markers as the map bounds are changed. The problem is that when I drag the map around for a while and then end dragging a flood of events are triggered at once. They appear to be queueing up while the map is being dragged.

Is there some way I can add a timer to stop this or will I have to use the zoom_changed and dragend events as a workaround?

Here is the relivant code:

google.maps.event.addListener(map, 'bounds_changed', function() {
  var bounds_url = map.getBounds().toUrlValue();
  $.ajax({
    //... 
  });
});

Upvotes: 2

Views: 1249

Answers (2)

varun
varun

Reputation: 4650

This is known bug , google team recommends using:

google.maps.event.addListener(map, 'idle', function() { });

Upvotes: 1

Himanshu Khurana
Himanshu Khurana

Reputation: 585

Add a timeout, that runs your code 500ms after the event fires, each time the event fires clear the timeout and create a new one.

google.maps.event.addListener(map, 'bounds_changed', (function () {
    var timer;
    return function() {
        clearTimeout(timer);
        timer = setTimeout(function() {
            // here goes an ajax call
        }, 500);
    }
}()));

Upvotes: 1

Related Questions