Callback on Load

3,226 views
Skip to first unread message

Peter Robinett

unread,
Jun 2, 2009, 8:45:43 PM6/2/09
to Google Maps JavaScript API v3
In v2 we would pass a callback to the Maps JS so we would know when we
can start creating our map. If I pass a callback to v3 (e.g.
http://maps.google.com/maps/api/js?sensor=false&callback=myCallback),
it does not work how I expected and, in fact, I don't know if it works
at all.

I expected myCallback() to be called at the end of the JS file, once
everything is loaded. Instead, it is called within google.maps.Load()
and I don't see anything that called google.maps.Load() or any other
of the google.maps methods within the v3 JS file.

Digging deeper, if I don't include a callback parameter when I request
the JS, the callback line is naturally missing, but there is also an
additional line at the very end of the file: google.maps.loadScripts
(). google.maps.loadScripts() loads a JS file that in turn calls
google.maps.Load() at the end of its execution sequence.

Since google.maps.loadScripts() is never called in the version of the
v3 JS file with a callback, google.maps.Load() is never called. This
suggests to me that the call to google.maps.loadScripts() at the end
of the v3 Javascript file is being incorrectly omitted.

Am I correct? I have cross-posted this in the Issue Tracker as a v3
bug (#1355).

Peter Robinett

pamela (Google Employee)

unread,
Jun 5, 2009, 8:28:03 PM6/5/09
to google-map...@googlegroups.com
Hi Peter -

You can do dynamic loading, but it's done a bit differently than
before. Now, you specify the callback in the boostrap request, load
the bootstrap, and then call google.maps.LoadScripts(). I've put
together 2 examples that show how you can use it:
http://gmaps-samples-v3.googlecode.com/svn/trunk/callback/

- pamela

alfonsoml

unread,
Jun 6, 2009, 4:32:28 PM6/6/09
to Google Maps JavaScript API v3
Hi Pamela

If I'm not mistaken both example are really the same.
The first one calls "google.maps.loadScripts()" upon clicking the
button.
The "delayed" in theory loads the bootstrap at that moment, but the
fact is that the script is already loaded in the head of the document,
and after adding the script again at the body (that doesn't improve
anything) it also calls google.maps.loadScripts();

function appendBootstrap() {
var script = document.createElement("script");
script.src = "http://maps.google.com/maps/api/bootstrap?
sensor=false&callback=handleApiReady";
document.body.appendChild(script);
google.maps.loadScripts();
}

Removing the script in the <head> obviously makes any call to
google.maps fail as no google object has been defined until the
bootstrap file is loaded, but the callback parameter is only used to
signal which function to run after the google.maps has been loaded.

Could it be possible to add support for another parameter to specify a
function to run after the bootstrap script has been loaded?

Or is it a bug at the server?, because both the "js" and "bootstrap"
scripts return exactly the same
http://maps.google.com/maps/api/bootstrap?sensor=false&callback=handleApiReady
http://maps.google.com/maps/api/js?sensor=false&callback=handleApiReady

Thanks.

On Jun 6, 2:28 am, "pamela (Google Employee)" <pamela...@gmail.com>
wrote:

pamela (Google Employee)

unread,
Jun 6, 2009, 4:49:33 PM6/6/09
to google-map...@googlegroups.com
Hi alfonsoml-

Good catch. I've updated that example to work, but it relies on
detecting when the script has been loaded, and unfortunately IE
doesn't support the 'onload' handler, so the code gets a bit long.

The 'bootstrap' is an alias for 'js' (or viceversa) - please use 'js',
I've updated the examples to use that.

Would you want another callback function parameter, or would you
prefer that google.maps.loadScripts() was called by us, and then the
callback= function triggered?

- pamela

bratliff

unread,
Jun 7, 2009, 8:40:44 AM6/7/09
to Google Maps JavaScript API v3
On Jun 6, 8:49 pm, "pamela (Google Employee)" <pamela...@gmail.com>
wrote:
> Hi alfonsoml-
>
> Good catch. I've updated that example to work, but it relies on
> detecting when the script has been loaded, and unfortunately IE
> doesn't support the 'onload' handler, so the code gets a bit long.

I believe IE supports an "onreadystatechange" event for dynamically
loaded scripts. FF supports "onload" & "onerror".

Peter Robinett

unread,
Jun 7, 2009, 4:17:58 PM6/7/09
to Google Maps JavaScript API v3
Personally I would prefer that google.maps.loadScripts() is called by
the script and then the callback function is triggered. I assume that
people that need to ensure the proper environment for their callback
is setup would do so by adding the script tag once the environment is
ready, rather than by calling google.maps.loadScripts() later.

Peter Robinett

On Jun 6, 1:49 pm, "pamela (Google Employee)" <pamela...@gmail.com>
wrote:
> >http://maps.google.com/maps/api/bootstrap?sensor=false&callback=handl...

pamela (Google Employee)

unread,
Jun 7, 2009, 5:22:57 PM6/7/09
to google-map...@googlegroups.com
Hi Peter-

I've turned your issue into a feature request for just that:
http://code.google.com/p/gmaps-api-issues/issues/detail?id=1355

- pamela
Reply all
Reply to author
Forward
0 new messages