Event bounds_changed triggers too often, problem

4,557 views
Skip to first unread message

Jurgis Stepanovs

unread,
Aug 13, 2010, 1:49:47 AM8/13/10
to Google Maps JavaScript API v3
Hi!

I use code bellow to read markers from DB, when user visible map area
changes.
...
google.maps.event.addListener(map, 'bounds_changed', function() { //
bounds_changed or zoom_changed or center_changed
readDataFromDB;
});

...
But serious problem is that listener executes to often 3 or 4 times
when expecting only 1, and my
DB server gets huge loads of unnecessary queries. I think most on fast
mouse wheeling (zooming in and out).

How to set this trigger to work more slowly?

I tried setTimeout(readDataFromDB(),2000), but it seems not to do
right thing, because
I suppose that during that timeout happens more bounds_change events
that waits
for JavasScript code execution loop. At the end code is executed also
3,4 times, but with 2sec
delay.

Todays Internet user is a lot using mouse wheel, and a lots moving
around with mouse, and it will kill my application if things will not
change ;)

I suggest Google API 3 engineers to implement this timeout option
directly
in the core of 'bounds_changed' listener.

May be someone has developed solution already?

PS I’m testing app on Chrome and IE …


Luke Mahé

unread,
Aug 13, 2010, 2:02:45 AM8/13/10
to google-map...@googlegroups.com
Hi Jurgis,

Instead of bounds_changed you can listen to 'idle' or if you want to go down the timeout approach, you will need to cancel the timeout before creating another one.

var myTimeout;
google.maps.event.addListener(map, 'bounds_changed', function() {
  if (myTimeout) {
    window.clearTimeout(myTimeout);
  }

  myTimeout = window.setTimeout(readDataFromDB, 2000);
});

Hope this helps

-- Luke




--
You received this message because you are subscribed to the Google Groups "Google Maps JavaScript API v3" group.
To post to this group, send email to google-map...@googlegroups.com.
To unsubscribe from this group, send email to google-maps-js-a...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-maps-js-api-v3?hl=en.


Jurgis Stepanovs

unread,
Aug 13, 2010, 2:12:46 AM8/13/10
to Google Maps JavaScript API v3
Thanks, Luke! I'll try to implement your solution, after some cups of
coffee :)
> > google-maps-js-a...@googlegroups.com<google-maps-js-api-v3%2B unsub...@googlegroups.com>
> > .

Drew Noakes

unread,
Sep 22, 2010, 1:07:58 PM9/22/10
to Google Maps JavaScript API v3
Note that a new event has been added to take care of this. You don't
need to write this kind of hack any more.

Read about it here: http://code.google.com/p/gmaps-api-issues/issues/detail?id=1371

google.maps.event.addListener(map, 'idle', function(event) {
// fires when movement has stopped
});
> > google-maps-js-a...@googlegroups.com<google-maps-js-api-v3%2B unsub...@googlegroups.com>
> > .
Reply all
Reply to author
Forward
0 new messages