<?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>Random Musings of a Code Monkey &#187; Google</title>
	<atom:link href="http://www.tinyblueplanet.com/code/?feed=rss2&#038;tag=google" rel="self" type="application/rss+xml" />
	<link>http://www.tinyblueplanet.com/code</link>
	<description>Little bits of technology all broken down</description>
	<lastBuildDate>Wed, 07 Dec 2011 08:29:45 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.42</generator>
	<item>
		<title>The technology war</title>
		<link>http://www.tinyblueplanet.com/code/?p=27</link>
		<comments>http://www.tinyblueplanet.com/code/?p=27#comments</comments>
		<pubDate>Thu, 15 Sep 2011 09:49:13 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[Musings]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[SOAP]]></category>

		<guid isPermaLink="false">http://www.tinyblueplanet.com/code/?p=27</guid>
		<description><![CDATA[I recently attended some Google demo days and two interesting little facts were buried in the piles of other data. JSON versus XML Seems that Google is changing many of its APIs to no longer return data in XML fragments but to &#8230;<p class="read-more"><a href="http://www.tinyblueplanet.com/code/?p=27">Read more &#187;</a></p>]]></description>
				<content:encoded><![CDATA[<p>I recently attended some Google demo days and two interesting little facts were buried in the piles of other data.</p>
<h2>JSON versus XML</h2>
<p>Seems that Google is changing many of its APIs to no longer return data in XML fragments but to use <a href="http://www.json.org/">JSON</a> (Javascript Object Notation) to exchange data. Google claims its about 34% lighter in terms of bytes served to return JSON results than XML.</p>
<h2>SOAP versus REST</h2>
<p>Another technology change seems to be the large-scale move to prefer REST (Representational State Transfer) rather than SOAP for web services. I wasn&#8217;t aware there was a huge debate here. But looking online afterwards, there are endless flamewars, logs etc about this. But in a nutshell, if you don&#8217;t know about REST then its time to climb on the wagon as the Google machine is now all about REST architectures.</p>
<ul>
<li><a href="http://en.wikipedia.org/wiki/Representational_state_transfer">http://en.wikipedia.org/wiki/Representational_state_transfer</a></li>
<li><a href="http://www.ibm.com/developerworks/webservices/library/ws-restful/">http://www.ibm.com/developerworks/webservices/library/ws-restful/</a></li>
</ul>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tinyblueplanet.com/code/?feed=rss2&#038;p=27</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Google Maps Javascript API &#8211; An introduction with a focus on handling InfoWindows</title>
		<link>http://www.tinyblueplanet.com/code/?p=23</link>
		<comments>http://www.tinyblueplanet.com/code/?p=23#comments</comments>
		<pubDate>Thu, 15 Sep 2011 09:38:32 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Maps]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[InfoWindows]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.tinyblueplanet.com/code/?p=23</guid>
		<description><![CDATA[Using the Google Maps Javascript API Having final updated some map examples I had to version 3 of the Google Maps Javascript API, I ran into a few problems with the way InfoWindows had changed and as I searched for &#8230;<p class="read-more"><a href="http://www.tinyblueplanet.com/code/?p=23">Read more &#187;</a></p>]]></description>
				<content:encoded><![CDATA[<h2>Using the Google Maps Javascript API</h2>
<p>Having final updated some map examples I had to version 3 of the <a href="http://code.google.com/apis/maps/">Google Maps Javascript API</a>, I ran into a few problems with the way InfoWindows had changed and as I searched for solutions, it seems many people are having problems with this part of the API.</p>
<p>In version 3 there was a move towards simplification, reducing in some instances what Google Maps did for you; most of these changes were seem to be around simplifying the API to make it lightweight for mobile devices. However, because of these changes you now need to do a little extra work yourself.</p>
<p>On the other hand, you no longer need an API key to use the scripts, which is a really nice change.</p>
<p>Below, I generated a few simple examples showing how to use the API and especially how to handle markers and their infowindows (the little popup quote bubbles common on Google Maps).</p>
<h2>A Simple Marker Example</h2>
<p>Lets look at the simplest example of adding a marker to a map (this is based on <a href="http://code.google.com/apis/maps/documentation/javascript/examples/marker-simple.html">http://code.google.com/apis/maps/documentation/javascript/examples/marker-simple.html</a>):</p>
<p>First we include the new Javascript API in the header (no API key required which is great):</p>
<pre>&lt;script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"&gt;&lt;/script&amp;gt</pre>
<p>Then we set our HTML to call a Javascript function, <strong>initialize</strong> to handle the map, along with setting up a div element into which we want to draw our map. In this example, we setting the div id to be <strong>map_canvas</strong>. Note the CSS styling of height and width is just there to constrain the size of the map we&#8217;re drawing.</p>
<pre>&lt;body onload="initialize()"&gt;
&lt;div id="map_canvas" style="width: 450px; height: 450px;"&gt;&lt;/div&gt;
&lt;/body&gt;</pre>
<p>And now the fun part, here is the <strong>initialize</strong> function which we use to set-up our map:</p>
<pre>function initialize() {
    var myLatlng = new google.maps.LatLng(-0.09516,34.74733);

    var myOptions = {
        zoom: 10,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.TERRAIN
    }

    var myMap = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

    var myMarker = new google.maps.Marker({
        position: myLatlng,
        map: myMap,
        title:"Hello there Kisumu!"
    });
}</pre>
<p>This is broken down into four key components:</p>
<ul>
<li>A geographical data point (<strong>myLatLng</strong>), which is a <a href="http://code.google.com/apis/maps/documentation/javascript/reference.html#LatLng">LatLng</a> object.</li>
<ul>
<li>We want to point to a given place on the globe, in this case Kisumu, Kenya</li>
</ul>
<li>Options for the map we want to display (<strong>myOptions</strong>), which is based on the <a href="http://code.google.com/apis/maps/documentation/javascript/reference.html#MapOptions&quot;">MapOptions</a> object.</li>
<ul>
<li>There are plenty of options to set, but these are the required options. <strong>Zoom</strong> controls the zoom level of the map, the larger the number the closer in the map is zoomed. <strong>Center</strong> sets the center point of the map, in this case we&#8217;re using the latlng point we created. <strong>MapTypeId</strong> sets the map type, which can be one of: <em>HYBRID</em> (overlay of streets onto satelite images), <em>ROADMAP</em> (the more common street map view), <em>SATELLITE</em> (the Google Earth view) or <em>TERRAIN</em> (shows terrain and vegetation).</li>
</ul>
<li>The Map itself (<strong>myMap</strong>), a <a href="http://code.google.com/apis/maps/documentation/javascript/reference.html#Map">Map</a> object.</li>
<ul>
<li>We create a map object, telling it where to draw itself via the id we set (<strong>map_canvas</strong>) in the div in the HTML and we give the map the options we set-up.</li>
</ul>
<li>A display marker (<strong>myMarker</strong>), a <a href="http://code.google.com/apis/maps/documentation/javascript/reference.html#Marker">Marker</a> object.</li>
<ul>
<li>Finally we create a simple marker telling it to point at our LatLng point on our map and with a given tooltip title. If you place the cursor over the point, you can see the title appear.</li>
</ul>
</ul>
<p>This will create a map like this:</p>
<p><img class="aligncenter" title="Our first simple map" src="http://www.tinyblueplanet.com/code/examples/maps/simple_map.jpg" alt="" width="300" height="301" /></p>
<p>You can check out the map here: <a href="http://www.tinyblueplanet.com/code/examples/maps/simple_map.html">examples/maps/simple_map.html</a>. Show the source in your browser to see how it ties together. Feel free to make a copy and start playing around.</p>
<h2>Adding an InfoWindow</h2>
<p>In the previous example we have a map but this map is not very exciting. Typically, people want to be able to click on the points we&#8217;ve marked and show some extra information. This is done via Maps&#8217; <a href="http://code.google.com/apis/maps/documentation/javascript/reference.html#InfoWindow">InfoWindow</a> object, which handles the pop-up quote window common on Google Maps.</p>
<p>First, lets create a new InfoWindow object, like so:</p>
<pre>    var myContentString = "&lt;div id='pop_up'&gt;&lt;h2&gt;Kisumu, Kenya&lt;/h2&gt;&lt;p&gt;Kisumu is Kenya's 3rd largest city&lt;/p&gt;&lt;/div&gt;";

    var myInfowindow = new google.maps.InfoWindow({
        content: myContentString;
    });</pre>
<p>Notice that the content of an InfoWindow can be HTML so you can style it, format it, include images, provide links etc to really have some rich data in there. Note: there is a max size for this content, so don&#8217;t include too much.</p>
<p>Next, we attach a listener to our marker (<strong>myMarker</strong>) to open this infowindow when the marker is clicked:</p>
<pre>google.maps.event.addListener(marker, 'click', function() {
    myInfowindow.open(myMap,myMarker);
});</pre>
<p>This gives a map with a nice InfoWindow:</p>
<p><img class="aligncenter" title="A simple map with an InfoWindow" src="http://www.tinyblueplanet.com/code/examples/maps/simple_map2.jpg" alt="" width="300" height="301" /></p>
<p>You can check this out here: <a href="http://www.tinyblueplanet.com/code/examples/maps/simple_map2.html">examples/maps/simple_map2.html</a></p>
<p>You may noticed that clicking on the marker brings up the InfoWindow popup but you have to click on the InfoWindow close box to close it. We could also change our click handler to automatically close the window if we click on the marker, using this code:</p>
<pre>if (myInfowindow.getMap()) {
    myInfowindow.close();
}
else {
    myInfowindow.open(myMap,myMarker);
}</pre>
<p>That call to getMap will only succeed if the InfoWindow is open. There may be a better way to do this but so far this is the most reliable method I&#8217;ve found.</p>
<h2>Adding Multiple Markers with InfoWindows</h2>
<h2><span class="Apple-style-span" style="color: #333333; font-size: 13px; font-weight: normal; line-height: 18px;">There is some confusion about how to handle multiple markers and InfoWindows. In the old API there was only ever one InfoWindow and you had a global array of markers to hand to query. In the new v3 API things are little more complex.</span></h2>
<p>So there seems to be two ways to do this: multiple markers &amp; multiple infowindows or multiple markers &amp; a single infowindow.</p>
<h3>Multiple Markers &amp; Multiple InfoWindows</h3>
<p>We&#8217;re going to load our markers out of a static array to keep things simple. However, you can easily load out of an XML, KML or other file. Theres examples of loading out of an XML file here: <a href="http://www.tinyblueplanet.com/maps">http://www.tinyblueplanet.com/maps</a>.</p>
<p>First we&#8217;ll define some markers, giving the latitude and longitude, a title string and some info window contents.</p>
<pre>var locations = [
    [-0.09516,34.74733, 'Point 1', '&lt;p&gt;This is Point 1&lt;/p&gt;'],
    [-0.09526,34.75733, 'Point 2', '&lt;p&gt;This is Point 2&lt;/p&gt;'],
    [-0.09506,34.73733, 'Point 3', '&lt;p&gt;This is Point 3&lt;/p&gt;']
];</pre>
<p>Then we&#8217;ll iterate over the markers and add them to our map, using a new <strong>setmarker</strong> function:</p>
<pre>function setmarker(myMap, lat, lng, title, content) {
    var markerLatlng = new google.maps.LatLng(lat, lng);

    var myMarker = new google.maps.Marker({
        position: markerLatlng,
        map: myMap,
        title: title
    });

    var myInfowindow = new google.maps.InfoWindow({
        content: content
    });

    google.maps.event.addListener(myMarker, 'click', function() {
        if (myInfowindow.getMap()) {
            myInfowindow.close();
        } else {
            myInfowindow.open(myMap, myMarker);
        }
    });
}</pre>
<p>Theres not a lot new here, we turn our lat &amp; lng information into a <em>LatLng</em> object, create a marker for it, we then create a new info window object and use it in the click listener for the marker.</p>
<p>However, because we have multiple markers we are also creating multiple info windows; one on each call to the <strong>setmarker</strong> function. Each marker is then associated with each infowindow when we create the callback click listener.</p>
<p>You can see this in the example as InfoWindows will remain open until you close them:</p>
<p><img class="aligncenter" title="A simple map with multiple markers and infowindows " src="http://www.tinyblueplanet.com/code/examples/maps/simple_map3.jpg" alt="" width="300" height="299" /></p>
<p>You can play with this example here: <a href="http://www.tinyblueplanet.com/code/examples/maps/simple_map3.html">examples/maps/simple_map3.html</a></p>
<h3>Multiple Markers and a Single Info Window</h3>
<p>Finally, we&#8217;ll look at how to achieve the old style of InfoWindows, where you had just a single window, which involves a little more effort.</p>
<p>First we move creation of the InfoWindow out of the <strong>setMarker</strong> function and just create a single empty one:</p>
<pre>var myInfowindow = new google.maps.InfoWindow();</pre>
<p>Next we change the <strong>setmarker</strong> function as follows:</p>
<pre>function setmarker(myMap, myInfowindow, locationId, lat, lng, title) {
    var markerLatlng = new google.maps.LatLng(lat, lng);

    var myMarker = new google.maps.Marker({
        position: markerLatlng,
        map: myMap,
        title: title
    });

    google.maps.event.addListener(myMarker, 'click', (function(myMarker, locationId) {
        return function() {
            myInfowindow.setContent(locations[locationId][3]);
            myInfowindow.open(myMap, myMarker);
        }
    })(myMarker, locationId));
}</pre>
<p>This code was quickly written so please allow me some flunkiness here (we could obviously clean up that parameter list for a start).</p>
<p>Basically we now pass our sole InfoWindow (<strong>myInfowindow</strong>) into the <strong>setmarker</strong> function along with the id (<strong>locationid</strong>) in the locations array. For simplicity we&#8217;ve moved the <strong>locations</strong> array outside of the <strong>initalize</strong> function so it can be found in the click callback, obviously that could be cleaned up to.</p>
<p>We now create a slightly different click callback, bound to our marker and the id of the location in the locations array. When the click is called, we associate the content of the correct marker (via the <strong>locationId</strong>) to the infowindow and then call to open the infowindow on the map associated with our marker.</p>
<p>Volia&#8230;</p>
<p><img class="aligncenter" title="A simple map with multiple markers and a single infowindow" src="http://www.tinyblueplanet.com/code/examples/maps/simple_map4.jpg" alt="" width="300" height="301" /></p>
<p>You can play with this example here <a href="http://www.tinyblueplanet.com/code/examples/maps/simple_map4.html">examples/maps/simple_map4.html</a></p>
<h2>What Next?</h2>
<ul>
<li><span class="Apple-style-span" style="color: #333333; font-size: 13px; font-weight: normal;">Find map examples you like and look at the Javascript via ShowSource in your browser. YOu can quickly figure out how people are using the API this way.</span></li>
<li>Try changing the default pin marker icon to something else. There are lots of options here (see MarkerImage and the Marker options).</li>
<li>Use Google Earth to make polygon shapes for your map (to show an area rather than a point)and more 3D displays on maps.</li>
<li>Use <a href="http://www.google.com/fusiontables/Home">Google Fusion Tables</a> to mash-up data to overlay geo data and other data.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.tinyblueplanet.com/code/?feed=rss2&#038;p=23</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
