<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>vijayjoshi.org &#187; google map</title>
	<atom:link href="http://www.vijayjoshi.org/tag/google-map/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.vijayjoshi.org</link>
	<description>php &#124; javascript &#124; ajax &#124; and all things web</description>
	<lastBuildDate>Sun, 20 Nov 2011 15:24:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>How to dynamically load the Google Maps javascript API (On demand loading)</title>
		<link>http://www.vijayjoshi.org/2010/01/19/how-to-dynamically-load-the-google-maps-javascript-api-on-demand-loading/</link>
		<comments>http://www.vijayjoshi.org/2010/01/19/how-to-dynamically-load-the-google-maps-javascript-api-on-demand-loading/#comments</comments>
		<pubDate>Tue, 19 Jan 2010 10:21:25 +0000</pubDate>
		<dc:creator>Vijay Joshi</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[google map]]></category>

		<guid isPermaLink="false">http://www.vijayjoshi.org/?p=481</guid>
		<description><![CDATA[
			
				
			
		
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: &#8220;JavaScript being pulled ...]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop --><!-- End Shareaholic LikeButtonSetTop --><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.vijayjoshi.org%2F2010%2F01%2F19%2Fhow-to-dynamically-load-the-google-maps-javascript-api-on-demand-loading%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.vijayjoshi.org%2F2010%2F01%2F19%2Fhow-to-dynamically-load-the-google-maps-javascript-api-on-demand-loading%2F&amp;source=v08i&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>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.<br />
This is a javascript pattern called <a title="On demand javascript" href="http://ajaxpatterns.org/On-Demand_Javascript" target="_self">On demand javascript</a>.</p>
<p>Bottomline is: <a title="On demand javascript summary" href="http://ajaxpatterns.org/On-Demand_Javascript#In_A_Blink" target="_self">&#8220;JavaScript being pulled from the server after the page has loaded</a>&#8220;.</p>
<p>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.</p>
<div class="mceTemp">
<dl id="attachment_453" class="wp-caption alignnone" style="width: 190px;">
<dt class="wp-caption-dt"><a title="Google maps ondemand loading demo" href="http://www.vijayjoshi.org/examples/gmap.html" target="_blank"><img class="size-full wp-image-453" style="border:0px" title="demo" src="http://www.vijayjoshi.org/wp-content/uploads/demo.png" alt="View Demo" width="180" height="45" /></a></dt>
</dl>
</div>
<p>As said above, <strong>DO NOT </strong>load any javascript api from Google using script tags. Instead we will use a button.</p>
<pre class="brush:html">
<input id="loadButton" onclick="loadAPI();" type="button" value="Load maps api" /></pre>
<p>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.</p>
<pre class="brush:js">function loadAPI()
{
    var script = document.createElement("script");
    script.src = "http://www.google.com/jsapi?key=YOUR_API_KEY_HERE&amp;callback=loadMaps";
    script.type = "text/javascript";
    document.getElementsByTagName("head")[0].appendChild(script);
}</pre>
<p>Do not forget to replace the API key above with your API key. (You can get an API key by <a title="Google maps API key" href="http://code.google.com/apis/maps/signup.html" target="_self">signing up here</a>)</p>
<p><strong>Important:</strong> Note the <strong>callback=loadMaps</strong> 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.</p>
<pre class="brush:js">function loadMaps()
{
    //AJAX API is loaded successfully. Now lets load the maps api
    google.load("maps", "2", {"callback" : mapLoaded});
}</pre>
<p>loadMaps function uses load method of google AJAX API to load a specific api. Above we loaded &#8220;maps&#8221; version &#8220;2&#8243;. 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.</p>
<pre class="brush:js">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);
    }
}</pre>
<p>That is all to it. I have created a demo along these lines. View source for code. <a title="Google maps ondemand loading demo" href="http://www.vijayjoshi.org/examples/gmap.html" target="_blank"><strong>You can see it here</strong></a>.</p>
<div class="shr-publisher-481"></div><!-- Start Shareaholic LikeButtonSetBottom --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:right;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.vijayjoshi.org%2F2010%2F01%2F19%2Fhow-to-dynamically-load-the-google-maps-javascript-api-on-demand-loading%2F' data-shr_title='How+to+dynamically+load+the+Google+Maps+javascript+API+%28On+demand+loading%29'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom -->]]></content:encoded>
			<wfw:commentRss>http://www.vijayjoshi.org/2010/01/19/how-to-dynamically-load-the-google-maps-javascript-api-on-demand-loading/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>

