<?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>Kickass Labs &#187; Google Maps</title>
	<atom:link href="http://www.kickasslabs.com/category/google-maps/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.kickasslabs.com</link>
	<description>We &#9829; code.</description>
	<lastBuildDate>Wed, 28 Dec 2011 16:57:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Google Maps events in Mobile Safari and PhoneGap for iPhone</title>
		<link>http://www.kickasslabs.com/2009/04/29/google-maps-events-in-mobile-safari-and-phonegap-for-iphone/</link>
		<comments>http://www.kickasslabs.com/2009/04/29/google-maps-events-in-mobile-safari-and-phonegap-for-iphone/#comments</comments>
		<pubDate>Wed, 29 Apr 2009 20:23:27 +0000</pubDate>
		<dc:creator>Brad</dc:creator>
				<category><![CDATA[Google Maps]]></category>
		<category><![CDATA[hacks]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Events]]></category>
		<category><![CDATA[gmaps]]></category>

		<guid isPermaLink="false">http://www.kickasslabs.com/?p=255</guid>
		<description><![CDATA[Having trouble getting your Google Maps div to respond to events like you want in Mobile Safari or a PhoneGap app for iPhone? So was I. Disabling pinch-zoom is simple enough, but getting a finger drag to (a) not move &#8230; <a href="http://www.kickasslabs.com/2009/04/29/google-maps-events-in-mobile-safari-and-phonegap-for-iphone/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Having trouble getting your <a href="http://code.google.com/apis/maps/" target="gmaps" title="Google Maps">Google Maps</a> div to respond to events like you want in Mobile Safari or a <a href="http://www.phonegap.com/" target="phonegap" title="PhoneGap">PhoneGap</a> app for iPhone?  So was I.  Disabling pinch-zoom is simple enough, but getting a finger drag to (a) not move the map and (b) draw something on the map was a major pain.  I share my solution below:  Proxying events through a transparent div overlaid on the Google Maps div.</p>
<p>To play along at home, you&#8217;ll need your own <a href="http://code.google.com/apis/maps/signup.html" target="gmaps_key" title="Google Maps API Key signup">Google Maps key</a> or you can just aim your mobile browser to my <a href="http://kickasslabs.com/examples/gmap_events.html" target="gmaps_example" title="Google Maps Event Proxying Example">example code at http://kickasslabs.com/examples/gmap_events.html</a>.  (Note that this will <i>not</i> work in a regular desktop browser.)  Besides a <tt>&lt;script&gt;</tt> tag importing the GMaps JavaScript, you should not need any other libraries or tools besides Mobile Safari to view and use the example page.  If the page is zoomed way out when you start, just double-tap the map tile in the upper-left corner &#8211; it should zoom to fill your viewport.</p>
<p>What you should see is a map tile with a line painted on it.  If you touch the screen, you should see one end of the line follow your finger.</p>
<p>It looks simple but I had a bit of trouble getting it to work, because touch events are not like mouse events (curious as to <a href="http://www.quirksmode.org/blog/archives/2008/08/iphone_events.html" target="mouse_vs_touch" title="Mouse vs. Touch">why?</a>), and GMaps doesn&#8217;t respond to touch events by default.</p>
<p><b>Step by Step</b></p>
<p>First, you need 2 divs:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">    &lt;!-- This div holds our map --&gt;
    &lt;div id=&quot;map&quot; style=&quot;width: 320; height: 320px&quot;&gt;&lt;/div&gt;
&nbsp;
    &lt;!-- This div lies on top of the map and acts as an event proxy --&gt;
    &lt;div id=&quot;mapoverlay&quot; style=&quot;height:320px; width: 320px; position: absolute;&quot;&gt;&lt;/div&gt;</pre></div></div>

<p>And you need to position one div over the other:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">    mapDiv = document.getElementById('map');
    mapOverlayDiv = document.getElementById('mapoverlay');
    mapOverlayDiv.style.top = (mapDiv.offsetTop + 0) + 'px';
    mapOverlayDiv.style.left = (mapDiv.offsetLeft + 0) + 'px';</pre></div></div>

<p>You set up the map div like you would for any other GMaps-enabled page:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">    // the map
    _map = new GMap2(document.getElementById(&quot;map&quot;));
&nbsp;
    // coordinates for home base
    _lat = 37;
    _lng = -95;
    mapCenter = new GLatLng(_lat, _lng);
    _map.setCenter(mapCenter, 15);</pre></div></div>

<p>Set up the overlay div to respond to touch events by firing a custom event for your map, e.g.:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">    mapOverlayDiv.ontouchstart = function(e) {
      GEvent.trigger(_map, 'customTouchStart',
        (e.touches[0].pageX - mapDiv.offsetLeft),
        (e.touches[0].pageY - mapDiv.offsetTop));
    }</pre></div></div>

<p>Now have your map respond to that event:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">    GEvent.addListener(_map, 'customTouchStart', mapTouchStart);</pre></div></div>

<p>&#8230;where <tt>mapTouchStart</tt> is a callback function that does something useful in response to a touch:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">    function mapTouchStart(xPixel, yPixel) {
      redrawLine(xPixel, yPixel);
    }</pre></div></div>

<p>And&#8230; well, then you&#8217;re done.  My example also responds to the touchMove event and has a little code for drawing lines, but you&#8217;ve seen all you need to know to get event proxying up and running for your app.</p>
<p>Got a question?  Got working code for an easier way?  Drop me a line in the comments!</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.kickasslabs.com%2F2009%2F04%2F29%2Fgoogle-maps-events-in-mobile-safari-and-phonegap-for-iphone%2F&amp;title=Google%20Maps%20events%20in%20Mobile%20Safari%20and%20PhoneGap%20for%20iPhone" id="wpa2a_2"><img src="http://www.kickasslabs.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.kickasslabs.com/2009/04/29/google-maps-events-in-mobile-safari-and-phonegap-for-iphone/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

