views:

315

answers:

1

Hello all. I am using google maps v2 in my web application. I want to call database to fetch records when map bounds are changed due to zoom or drag. Until now I was using tilesloaded event handler to do it assuming the map bounds will change when all tiles are loaded (I want clarification on this assumption too).

When I do zoom in/out and handle zoomend event to set a flag which is checked in tilesloaded event handler which then queries database using new bounds. This works fine as long as tilesloaded event is fired AFTER zoomend event. But (annoyingly) many a times it is fired BEFORE zoomend event, so in that case the zoom flag wouldn't be set in tilesloaded event handler and no db call happens.

Same happens for drag. And also many times tilesloaded event is not fired when we drag map a little. Does google uses any cache mechanism for map tiles if we drag only a little (just a wild guess).

I want something like bounds_changed event to be fired for v2, please somebody help me out.

+2  A: 

I would use the moveend event. It will fire whenever the map is zoomed or moved (on mouseup).

var moveListener = GEvent.addListener(map, "moveend", function() {
    var bounds = map.getBounds();
    var sw = bounds.getSouthWest();
    var ne = bounds.getNorthEast();

    var sBound = sw.lat();
    var wBound = sw.lng();
    var nBound = ne.lat();
    var eBound = ne.lng();

    // AJAX call to the database with the map bounds
});
Chris B
Thanks for reply. I knew about moveend but I want to know whether in moveend event handler we will immediately get the latest bounds of map that is zoomed/dragged. Can you please elaborate on this? Thanks for reply again.
Prashant
@Prashant - The 'moveend' event fires as soon as the user releases the mouse, so you will definitely get the latest bounds.
Chris B
Thank you Chris. It's clear to me now.
Prashant