Wednesday, May 6, 2009

plugd and Google Maps

I just started looking into integrating Google Maps into an application. Given the breath of features offered by Google Maps, its no surprise that the JavaScript download is pretty hefty -> ~72KB with an empty cache for main.js. Also, including Google Maps in a page brings down around 20 extra images (depending on the default size of your map).

So, I started looking into ways for deterministically loading Google Maps as there are certain sections of the application I work on where Google Maps is not needed. Looking at the Google Ajax APIs, this seems like the easiest way to load Google Maps (and other Google APIs for that matter):


<script type="text/javascript"
src="http://www.google.com/jsapi?key=ABCDEFG"></script>

...

google.load("maps", "2");


Adding plugd into the mix, you can also cut down on loading the Google Ajax API JavaScript using the addScript method:


dojo.addScript("http://www.google.com/jsapi?key=" + key, dojo.hitch(this, function() {
var mapsLoaded = function() {
dojo.publish("/gmaps/loaded", []);
}
var langArray = dojo.locale.split("-");
google.load("maps", "2", {"callback" : mapsLoaded, "language": langArray[0] + "_" + langArray[1].toUpperCase()});
}));


And code which depends on Google Maps being loaded should listen to the "/gmaps/loaded" topic before trying to access any Google Maps APIs.

No comments:

Post a Comment