map init callback?

2,450 views
Skip to first unread message

timothyb

unread,
Jun 3, 2009, 4:05:01 AM6/3/09
to Google Maps JavaScript API v3
get_bounds():
"Returns the lat/lng bounds of the current viewport. If the map is not
yet initialized (i.e. the mapType is still null), or center and zoom
have not been set then the result is null."

I would think it's quite common for an early get_bounds() call as many
sites want to add overlays and to do this, they of course need to know
what the viewport is. I can't do a call to my DB until I know what
bounds to provide the SQL statement. Is there a callback to advise
when the map init has finished and we can do a get_bounds?

Cheers, Tim

Mike Williams

unread,
Jun 3, 2009, 4:17:20 AM6/3/09
to google-map...@googlegroups.com
There's an undocumented map "load" event.

In v2 that event gets triggered when the map setup is complete, and
isLoaded() would return true. This means position, zoom, and map type
are all initialized, but tile images may still be loading.

It's possible that this event might mean the same thing in v3.

--
Mike Williams
Gentleman of Leisure

Peter Robinett

unread,
Jun 3, 2009, 7:00:35 PM6/3/09
to Google Maps JavaScript API v3
There's also the documented tilesloaded event:
http://code.google.com/apis/maps/documentation/v3/reference.html#Map

Peter Robinett

String

unread,
Jun 4, 2009, 2:44:42 AM6/4/09
to Google Maps JavaScript API v3
On 4 June, 00:00, Peter Robinett <pe...@bubblefoundry.com> wrote:

> There's also the documented tilesloaded event:http://code.google.com/apis/maps/documentation/v3/reference.html#Map

Two problems with tilesloaded as a maps.loaded event:

1. It also fires every time the map needs to load _additional_ tiles
(on a pan, zoom, or resize). So if you only want it to fire the first
time, when the map has first loaded, you need to detach the listener
in the handler.

2. If one or more map tiles fail to load, this event won't fire.

String

Peter Robinett

unread,
Jun 4, 2009, 1:16:49 PM6/4/09
to Google Maps JavaScript API v3
Thanks, String, Point 1 explains some funny behavior I was seeing in
my code!

Peter Robinett

pamela (Google Employee)

unread,
Jun 5, 2009, 10:06:59 PM6/5/09
to google-map...@googlegroups.com
Hi Peter-

You should be able to use the bounds_changed event as well. Can you
give that a shot?
We may need to have a more general mechanism for the properties that
aren't available until after the view has loaded, depending how things
go.

- pamela

Peter Robinett

unread,
Jun 7, 2009, 8:08:27 PM6/7/09
to Google Maps JavaScript API v3
Thanks, Pamela, that seems to work well for me.

Peter

On Jun 5, 7:06 pm, "pamela (Google Employee)" <pamela...@gmail.com>
wrote:
> Hi Peter-
>
> You should be able to use the bounds_changed event as well. Can you
> give that a shot?
> We may need to have a more general mechanism for the properties that
> aren't available until after the view has loaded, depending how things
> go.
>
> - pamela
>

timothyb

unread,
Jun 14, 2009, 11:37:27 PM6/14/09
to Google Maps JavaScript API v3
The following code works for me too:
.
.
var boundsChangedListener;
.
.
// using bounds_changed to advise can get_bounds
boundsChangedListener = google.maps.event.addListener(map,
'bounds_changed', function() {

if (window.console) { console.log("bounds_changed"); }
download(); // <- function where I need map bounds

});
.
.
function download() {
// Remove the listener after map ready.
google.maps.event.removeListener(boundsChangedListener);
//boundsChangedListener = null;
.
// then do map stuff...
.

If I actually set the boundsChangedListener to null, the google code
fails at: main.js line 23 with an "a is null" error. Unsure why..maybe
there is some Google code that is still referring to my listener..

Stephen J. Fuhry

unread,
Jul 23, 2009, 10:55:04 PM7/23/09
to Google Maps JavaScript API v3
I placed a feature request for a load / init function here:

http://code.google.com/p/gmaps-api-issues/issues/detail?id=1516
Reply all
Reply to author
Forward
0 new messages