Blog Posts

Lazy Load Google Maps

Google Maps is a powerful API for including interactive maps on your website (but who didn't know that :)). But with features comes complexity and with complexity come larger startup times (at least in JavaScript land). But there are ways to improve the user experience for your visitors. To test the possibilities, I wrote a (very) little app, which displays POI-markers for the three Liip offices.

Prerequisite: I used some YUI components to make it easier, especially for displaying some time measurements. All common components are loaded before timing started, so that is not really an issue here:

<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.5.1/build/logger/assets/skins/sam/logger.css" />
<link rel="stylesheet" type="text/css" href="masync.css"/>
<script type="text/javascript" src="http://yui.yahooapis.com/2.5.1/build/yahoo-dom-event/yahoo-dom-event.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.5.1/build/logger/logger-min.js"></script>

The JavaScript files should actually be at the bottom of the html, if possible, but we don't obey that rule here.

Here the 4 ways I tested, click on the title to see the implementations in action (If you have firebug enabled, turn it off, otherwise it takes ages on firefox):

1) "no lazy load top" approach - the usual way

For the first attempt, I took the "usual" way. Putting the inclusion of the google map JavaScript file into the <head> element. But when a JS file is loaded, the browser won't load anything else, until that one is finished and therefore won't for example load and display the little icon I put on the page. The important load times are:

Time until image loaded: 327ms
Time until domready fires: 316ms
Time until window.onload fires: 390ms
Time until map.load fires: 274ms

It takes pretty long, until something appears on the webpage, as loading and parsing the google javascript takes precedence.

2) "no lazy load bottom" approach - a more recommended way

Instead of putting the big google map javscript file in the head, we put it at the end of the </body> part. That improves response for the visitor a lot.

Time until image loaded: 11ms
Time until domready fires: 331ms
Time until window.onload fires: 461ms
Time until map.load fires: 290ms

While domready and onload are not sooner than in the first approach (even take longer), the image from the content part is loaded much faster and loaded almost immediately. Your user will see the whole page, just has to wait for the map a little bit longer. The effect is the more noticeable the slower your connection is, since the image loading part doesn't have to wait until the file from google is loaded.

3) "direct lazy load" approach - using the Google AJAX API

With the Google AJAX API, you can load the whole google maps code on demand. For this third approach I just did this, after the window.onload event fires. This leads to the following times:

Time until image loaded: 11ms
Time until domready fires: 41ms
Time until window.onload fires: 47ms
Time until map.load fires: 352ms

As you can imagine, the domready and window.onload events now fire much much sooner, but the total time until the map is ready takes a little bit longer. If your page relies on having domready and/or window.onload ready as soon as possible (for other components than the map), this could be a way to improve the end user experience. They don't have to wait until the map is loaded for other dynamic components to appear.

4) "on demand lazy load" approach - using Google Static Maps API

The last approach is not loading the google maps javascript by default, but only on demand. With the help of the Google Static Maps API you can even show a map, without having to execute lots of javascript code (but unfortunately only street maps currently and no satellite). As some user may nevertheless wish to zoom and pan into the map, we load the whole interactive code as soon as the user moves the mouse over the map. On a decent internet connection, this should be fast enough and not really noticeable (except that the street maps are not exactly the same :)). This gives you the responsiveness of a fast static website plus the interactivity of a google maps based site, if the user wishes so. Of course, this later-on-demand loading isn't really what you want, if the map is the central part of your site :)

Time until image loaded: 16ms
Time until domready fires: 52ms
Time until window.onload fires: 61ms
Time until map.load fires: n/a

domready et al. take a little bit longer, because the static image from google has to be loaded, which the first three approaches don't.

Related Entries:
- Applying Niwea: TV Sélection iPad app
- YUI Training
- GottaGo is Number 2^H1 in the Swiss iTunes Store

Comments [16]

Yoan, 22.04.2008 14:22 CEST

You can do on demand lazy load with Yahoo! Maps kinda easily (and you should be able to do so with Google Maps but it's far more complex).

var oScript = document.createElement("script");
oScript.type="text/javascript";
oScript.src=<SRC>;
document.body.appendChild(oScript);

But, there is a but, all the .js you're including MUST NOT contain any "document.write". So, time to hack!

For Y!Maps: rewrite "function _ywjs(inc)" (ajaxymap) to not use document.write but the DOM-compliant method above, host it and you should be able to do on demand lazy load of maps. (brilliant!)

Google has a second document.write, so it's two levels of hacking and quite not feasible apart hacking the first level, hosting it and then having a proxy for the other one, something like: http://example.org/maps.google.com/... But I'm pretty sure that violate the end-user agreement you signed ;-)

Maybe you can host every files yourself. I didn't try that solution as I had to stick to Y! ones the time I did that.

Yahoo 1 - Google 0

Chregu, 22.04.2008 15:19 CEST

Thanks for the feedback, but what I don't get, why should a hack google's files to use on demand lazy loading, when there's already an official way (via the Google AJAX API) to do it? That's what i used here: http://trash.chregu.tv/lazyload/on-demand-lazy-load.php and especially http://trash.chregu.tv/lazyload/masync.js

Or am I missing something?

Yoan, 22.04.2008 16:09 CEST

Yes, very (very) nice, finally (even)! They added a third attribute to "q" and if this one is true, the DOM way is used. I can revise my opinion on who get the point between Y! and Big G ;-)

I tried doing that from a GM script last autumn and it was miserably blowing up. Now, it's perfect for doing mashups.

Thanks a lot!

Pamela Fox, 22.04.2008 20:40 CEST

It's not quite documented, but you don't actually have to use the AJAX API common loader to load the Maps API asynchronously. See this example:
http://gmaps-samples.googlecode.com/svn/trunk/dynamicloading/scriptappend.html

Also see Barry's examples of dynamic loading (with static maps as well):
http://www.nearby.org.uk/google/static3.php

We'll document the async/callback parameters soon.

Chregu, 22.04.2008 21:34 CEST

Thank you Pamela for the update. I'll try that. Even less code to load, great :)

Sim, 29.04.2008 11:41 CEST

How to put progress bar while loading markers in the google map?
Thx.

Mikael, 05.05.2008 08:39 CEST

Hmm, all very interesting posts, thank you guys.

Justin Smith, 17.06.2008 08:52 CEST

What a fantastic page!

The page in the URL uses an included file with the map config info which I took frtom your Direct lazy load.

However there appears to be an error when I check it in firefox called.

loadBarrier is not defined

Can you tell me what I have done wrong?

Thanks

Justin

Michel, 21.08.2008 23:05 CEST

Pamela,
Is it possible to do "Barry's trick" (http://www.nearby.org.uk/google/static3.php) with 2 maps on 1 page? I tried it, but it goes wrong when clicking the second map: http://home.deds.nl/~thelarch/
And how can I start with hybrid (static) maps from the beginning?
Thanks in advance.
Greetings,
Michel

Aamir Afridi, 27.08.2008 15:56 CEST

Will it be an effect way to add the file when page loads completely with jQuery something like:


$(document).ready(function() {
$('head').appent("<script src='http://maps.google.com/maps?file=api&v=2&key=yourKeyGoHere" type="text/javascript'>");
});

Chregu, 27.08.2008 16:01 CEST

Aamir: I don't know, but why use that, if you can use Google's perfectly working AJAX API

Aamir Afridi, 27.08.2008 16:12 CEST

@Chregu:
I know this but loading the js file (if included in <head>) takes alot of time. I am not loading the map on page load but after clicking a link the map will apear. I want google map external file on every page and it makes the whole website very very slow.

Also there are other js files which i don't want if there is no javascript support on the browser.

Poland, 24.11.2008 19:35 CEST

Thanks, a genius article! By the way, I read through this about five times before I realized I could click on the underlined quoted text to see your code. Could you please make the links more obvious? One really needs to see your code to truly understand what you did.

Tom, 31.03.2011 06:37 CEST

None of the links to the examples work anymore. All I get is 'The website declined to show this webpage'. This entry may be a few years old, but that is no reason to have bad links.

Chregu, 04.04.2011 16:37 CEST

Tom: The links are fixed, they work now again

jdsd, 08.04.2011 11:53 CEST

adasd

Add a comment

Your email adress will never be published. Comment spam will be deleted!