vijayjoshi.org - php | javascript | ajax | and all things web

How to dynamically load the Google Maps javascript API (On demand loading)

Normally google maps js api is loaded at the time of page load via a script tag with src set. If map is not so major feature of your application or you want to reduce page download time, you can defer loading the javascript load it when required.
This is a javascript pattern called On demand javascript.

Bottomline is: “JavaScript being pulled from the server after the page has loaded“.

Google maps provide the facility to dynamically load the api when required.  So no need to load it at the time of page load. I will present an example here which will load the maps api on click of a button. Here is how to do it.

View Demo

As said above, DO NOT load any javascript api from Google using script tags. Instead we will use a button.

To load the api onclick of this button, create a script tag, set its src and append it to document. This will load the Google AJAX API required to load other google APIs.

function loadAPI()
{
    var script = document.createElement("script");
    script.src = "http://www.google.com/jsapi?key=YOUR_API_KEY_HERE&callback=loadMaps";
    script.type = "text/javascript";
    document.getElementsByTagName("head")[0].appendChild(script);
}

Do not forget to replace the API key above with your API key. (You can get an API key by signing up here)

Important: Note the callback=loadMaps in script.src. This is the thing doing all the magic. It tells which function to call when AJAX API has loaded. In above case it will call loadMaps function. So, let us define that.

function loadMaps()
{
    //AJAX API is loaded successfully. Now lets load the maps api
    google.load("maps", "2", {"callback" : mapLoaded});
}

loadMaps function uses load method of google AJAX API to load a specific api. Above we loaded “maps” version “2″. Again callback is the name of function that will be called when maps api is loaded. callback function will be called only when the maps api is loaded successfully.

function mapLoaded()
{
    //here you can be sure that maps api has loaded
    //and you can now proceed to render the map on page
    if (GBrowserIsCompatible())
    {
        var map = new GMap2(document.getElementById("map_canvas"));
        map.setMapType(G_SATELLITE_MAP);
        map.setCenter(new GLatLng(28.631466106808542, 77.07853317260742), 5);
    }
}

That is all to it. I have created a demo along these lines. View source for code. You can see it here.

Related Posts

Posted in : Javascript, Technology
Tags: , ,

11 Comments to “How to dynamically load the Google Maps javascript API (On demand loading)”

Add Comments (+)

  1. Akshath says:

    Brilliant !! Works perfect. Thanks.

  2. Christopher says:

    Thanks you very much. I was stumbling about this issue for how many hours already. This solution is pretty nice.

  3. Peter says:

    Thank you for sharing your kwno-how!
    I like this solution, it is clearly arranged and pretty simple.

  4. S.Ali Sadattalab says:

    Simple and great.
    you’re good writer man,
    thanks.

  5. Vijay Joshi says:

    Thank you guys. Good to know you found it useful.

  6. Robert Simon says:

    Thanks for this solution, it is very good!!!!

  7. Robert Simon says:

    Thanks so much, very useful!!!!

    How can I unload the map (along with API to free up resources) via button click?

  8. Atiq says:

    Please tell me how load version 3

  9. SoftConnection says:

    it works just fine, but it would be interesting to load the map without the aid of google load..

  10. Thanks for the great info – its helped me when doing my own map site. cheers :)

  11. dynamically, of course… (if the flash file is full-screen, can html in general be used inside a .swf?)

Trackbacks/Pingbacks

  1. Tweets that mention How to dynamically load the Google Maps javascript API (On demand loading) | vijayjoshi.org -- Topsy.com

Leave a Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

*

* Copy this password:

* Type or paste password here:

57,054 Spam Comments Blocked so far by Spam Free Wordpress

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>