Alex Rivera | Logout

Google Map event bounds_changed triggered multiple times when dragging

Asked 2010-12-02T18:31:56.237
69

I have a google map with markers. I want my markers to be refreshed when the map is moved/zoomed...

Google recommend to use the event bounds_changed for that, but when I move the map, the event is triggered for each pixel that I move the map. I want the map to be refreshed only when the user stopped moving the map, i.e. when he released the mouse button after dragging.

How can I do that ?

Edit
Report

2 Answers

135

It turns out it was a reported bug: http://code.google.com/p/gmaps-api-issues/issues/detail?id=1371.

The Google team recommend to use the event "idle". For example :

google.maps.event.addListener(map, 'idle', function() {
});
answered 2010-12-04T12:56:26.840
10

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);
    }
}()));

answered 2012-06-29T08:28:24.693

Your Answer