<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xml:base="http://labs.echoditto.com" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
 <title>EchoDitto Labs - Creating Google Mapplets - Comments</title>
 <link>http://labs.echoditto.com/google-mapplets</link>
 <description>Comments for &quot;Creating Google Mapplets&quot;</description>
 <language>en</language>
<item>
 <title>Mobile GMaps is a freeware</title>
 <link>http://labs.echoditto.com/google-mapplets#comment-4014</link>
 <description>&lt;p&gt;Mobile GMaps is a freeware (Creative Commons Attribution - NonCommercial - NoDerivs) that displays Google Maps and MSN Virtual Earth maps and satellite imagery on Java-enabled mobile phones, PDAs or other devices.&lt;/p&gt;
</description>
 <pubDate>Thu, 14 Aug 2008 00:28:03 -0700</pubDate>
 <dc:creator>arizona pool builder</dc:creator>
 <guid isPermaLink="false">comment 4014 at http://labs.echoditto.com</guid>
</item>
<item>
 <title>Creating Google Mapplets</title>
 <link>http://labs.echoditto.com/google-mapplets</link>
 <description>&lt;p&gt;Earlier this week Google Maps added &lt;a href=&quot;http://www.techcrunch.com/2007/07/11/google-to-launch-my-maps/&quot;&gt;Mapplets&lt;/a&gt; functionality.  The feature lets third-party developers offer their GMaps mashups as toggleable layers that can be added to the GMaps interface.&lt;/p&gt;

&lt;p&gt;In addition to my duties here at EchoDitto, I also do some work for DCist as a hobby &amp;mdash; in fact, I&#039;m pretty sure that my work on &lt;a href=&quot;http://www.dcist.com/map&quot;&gt;this&lt;/a&gt; Google Map of the DC subway system helped land me my job.  That app has languished, though, functional but limited and still stuck in version 1 of the GMaps API.&lt;/p&gt;

&lt;p&gt;So I took a stab at porting it over to the Mapplet format.  It went pretty well &amp;mdash; you can see the results by clicking &lt;a href=&quot;http://maps.google.com/ig/add?moduleurl=http://dcist.com/map/mapplet.xml&amp;pid=mpl&amp;synd=mpl&quot;&gt;here&lt;/a&gt;.  There were a number of API changes to contend with, though.&lt;/p&gt;

&lt;p&gt;First, there was the transition to the GMap2 object, which most of you have probably already done.  The Mapplet interface makes initialization much easier &amp;mdash; you can count on the map object to already be instantiated &amp;mdash; but other parts were confusing.  Most notably, Google appears to have finally un-switched the order of the latitude and longitude parameters in their function calls.  That&#039;s a welcome change, but figuring out how to undo (or double down on) the kludgy solution I was using made things pretty complicated.  The subway map was displaying in Antarctica for a little while.&lt;/p&gt;

&lt;p&gt;The actual Mapplet is implemented as an XML file based on the Google Gadget spec.  You can find an example &lt;a href=&quot;http://www.google.com/apis/maps/documentation/mapplets/#Hello_World_of_Mapplets&quot;&gt;here&lt;/a&gt;, in the Mapplet documentation.  It&#039;s a pretty straightforward format to work with, although documentation on some of the Google Gadget metadata tags can be a little unclear.&lt;/p&gt;

&lt;p&gt;There are two big changes from v2 of the GMaps API that developers should be aware of.  First, functions that query the map&#039;s state now must be made asynchronously.  There&#039;s no more GMap2.getSize(), so code like this won&#039;t work:&lt;/p&gt;

&lt;blockquote&gt;&lt;pre&gt;var MAP_WIDTH = map.getSize().width;&lt;/pre&gt;&lt;/blockquote&gt;

&lt;p&gt;Instead you must append &quot;Async&quot; to the method name and provide a callback function, like so:&lt;/p&gt;

&lt;blockquote&gt;&lt;pre&gt;var MAP_WIDTH = 0;
map.getSizeAsync(function(size){
   MAP_WIDTH = size.width;
});&lt;/pre&gt;&lt;/blockquote&gt;

&lt;p&gt;You can find more information about these asynchronous methods &lt;a href=&quot;http://www.google.com/apis/maps/documentation/mapplets/#Asynchronous&quot;&gt;here&lt;/a&gt;.  One unforeseen consequence can be that, depending on the speed of the asynchronous operation and your Javascript interpreter, some values may or may not be available when they&#039;re needed, opening the possibility of race conditions.  A simple way to solve this problem is to use timeouts to test the variable&#039;s availability:&lt;/p&gt;

&lt;blockquote&gt;&lt;pre&gt;var MAP_WIDTH = 0;
map.getSizeAsync(function(size){
   MAP_WIDTH = size.width;
});

function DoStuffOnceWidthIsAvailable()
{
   if(MAP_WIDTH&gt;0)
      DoTheAforementionedStuff(MAP_WIDTH);
   else
      setTimeout(DoStuffOnceWidthIsAvailable,100);
}

DoStuffOnceWidthIsAvailable();&lt;/pre&gt;&lt;/blockquote&gt;

&lt;p&gt;The second problem that presented itself is related to Google proxying AJAX queries.  My subway map uses &lt;a href=&quot;http://www.dcist.com/map/stations.xml&quot;&gt;an XML file&lt;/a&gt; to define the location of stations and subway lines.  The trickiest part of my code relates to drawing subway lines that run together, like the orange and blue lines: because lines in Google Maps are specified in terms of latitude and longitude, it&#039;s necessary to convert the pixel offset to degrees, and to do so in a direction perpendicular to the slope of the line.  It&#039;s all pretty simple geometry, but part of the operation involves taking a square root, which loses information about whether the slope of the line is positive or negative.  It&#039;s possible to figure this out with some more math, but I wanted to optimize my code as much as possible.  So instead I just specified an offset constant for each line segment in the XML file &amp;mdash; if the square root operation for that segment happens to return the wrong result (which it will either do or not do reliably), I just modify the constant in the XML file and the script multiplies the output appropriately.&lt;/p&gt;

&lt;p&gt;It&#039;s a little hacky, but it&#039;s fast.  However, it also means that getting the lines aligned properly involves a one-time commitment to fiddling with the XML, saving it, then checking it by reloading the map.  Most of the work was already done, but I needed to account for the Yellow Line&#039;s recent extension to Ft. Totten.  The fiddling wasn&#039;t going so well &amp;mdash; the changes weren&#039;t reliably showing up on the map, and I couldn&#039;t figure out what was going on.&lt;/p&gt;

&lt;p&gt;The answer, of course, was that Google was caching the file.  Because the Mapplet will be displayed on a web page hosted at a google.com address, your browser can only issue AJAX queries to Google.  They then have to go fetch the remote file &amp;mdash; in this case, the station XML file.  Google understandably caches the file to improve performance, and that was confusing the issue.&lt;/p&gt;

&lt;p&gt;But invalidating the cache was easy: just add a querystring.  Each time I made a change to the XML, I incremented a parameter:&lt;/p&gt;

&lt;blockquote&gt;&lt;pre&gt;var station_xml_url = &quot;http://www.dcist.com/map/stations.xml?q=1&quot;;
var station_xml_url = &quot;http://www.dcist.com/map/stations.xml?q=2&quot;;
var station_xml_url = &quot;http://www.dcist.com/map/stations.xml?q=3&quot;;
...&lt;/pre&gt;&lt;/blockquote&gt;

&lt;p&gt;And so on.  Google helpfully caches by the complete URL, so you&#039;ll get a new XML file each time.&lt;/p&gt;

&lt;p&gt;That&#039;s about it.  It was more work that I expected, but creating the Mapplet was still relatively painless.  Have a look through &lt;a href=&quot;http://www.google.com/apis/maps/documentation/mapplets/&quot;&gt;the documentation&lt;/a&gt; and get cracking &amp;mdash; I think the Mapplet concept promises to move mashups past novelty status and into everyday usefulness.&lt;/p&gt;</description>
 <comments>http://labs.echoditto.com/google-mapplets#comments</comments>
 <category domain="http://labs.echoditto.com/taxonomy/term/13">api</category>
 <category domain="http://labs.echoditto.com/taxonomy/term/70">gmaps</category>
 <category domain="http://labs.echoditto.com/taxonomy/term/69">google</category>
 <category domain="http://labs.echoditto.com/taxonomy/term/71">mapplet</category>
 <category domain="http://labs.echoditto.com/taxonomy/term/26">mashup</category>
 <pubDate>Fri, 13 Jul 2007 10:35:21 -0700</pubDate>
 <dc:creator>Tom</dc:creator>
 <guid isPermaLink="false">34 at http://labs.echoditto.com</guid>
</item>
</channel>
</rss>
