Sit back, relax, and enjoy the code.

Google Maps events in Mobile Safari and PhoneGap for iPhone

Posted: April 29th, 2009 | Author: Brad | Filed under: Google Maps, HTML, Javascript, Programming, hacks, iPhone | Tags: , , , , | 3 Comments »

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 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.

To play along at home, you’ll need your own Google Maps key or you can just aim your mobile browser to my example code at http://kickasslabs.com/examples/gmap_events.html. (Note that this will not work in a regular desktop browser.) Besides a <script> 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 – it should zoom to fill your viewport.

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.

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 why?), and GMaps doesn’t respond to touch events by default.

Step by Step

First, you need 2 divs:

    <!-- This div holds our map -->
    <div id="map" style="width: 320; height: 320px"></div>
 
    <!-- This div lies on top of the map and acts as an event proxy -->
    <div id="mapoverlay" style="height:320px; width: 320px; position: absolute;"></div>

And you need to position one div over the other:

    mapDiv = document.getElementById('map');
    mapOverlayDiv = document.getElementById('mapoverlay');
    mapOverlayDiv.style.top = (mapDiv.offsetTop + 0) + 'px';
    mapOverlayDiv.style.left = (mapDiv.offsetLeft + 0) + 'px';

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

    // the map
    _map = new GMap2(document.getElementById("map"));
 
    // coordinates for home base
    _lat = 37;
    _lng = -95;
    mapCenter = new GLatLng(_lat, _lng);
    _map.setCenter(mapCenter, 15);

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

    mapOverlayDiv.ontouchstart = function(e) {
      GEvent.trigger(_map, 'customTouchStart',
        (e.touches[0].pageX - mapDiv.offsetLeft),
        (e.touches[0].pageY - mapDiv.offsetTop));
    }

Now have your map respond to that event:

    GEvent.addListener(_map, 'customTouchStart', mapTouchStart);

…where mapTouchStart is a callback function that does something useful in response to a touch:

    function mapTouchStart(xPixel, yPixel) {
      redrawLine(xPixel, yPixel);
    }

And… well, then you’re done. My example also responds to the touchMove event and has a little code for drawing lines, but you’ve seen all you need to know to get event proxying up and running for your app.

Got a question? Got working code for an easier way? Drop me a line in the comments!

  • Share/Bookmark

3 Comments on “Google Maps events in Mobile Safari and PhoneGap for iPhone”

  1. #1 No One Thing / In Lieu of a Real Blog Post… said at 4:38 pm on April 29th, 2009:

    [...] buzzing, though, and in light of that I direct you to my most recent post at Kickass Labs, covering bending Google Maps to my will on an iPhone. Also of interest: Gabe has posted about a subtle gotcha in Rails [...]

  2. #2 VCosta said at 6:04 pm on December 13th, 2009:

    Hi,

    I’ve been trying to use phonegap and embedding a Google Map, but I either load the PhoneGap stuff, or the map. I can’t get both of them to load.

    I’m trying to use GMaps API V3,.

    Any tip or example?

    Thanks,

    VCosta

  3. #3 Brad said at 7:31 pm on December 13th, 2009:

    VCosta – Sorry, I don’t have any advice. My best guess is that there’s some sort of JavaScript conflict.

    I’d start by stripping down the problem to a bare-bones PhoneGap project bringing up a web page that does nothing but load a map with the Google Maps API. Verify that the app can load a simple page with no app (so you know that PhoneGap works) and that the page with the map loads correctly in Safari on the iPhone (so you know that you have that set up correctly). If all that works, but you still can’t get the map page to load in your PhoneGap app, I’d start asking around in Google Groups or StackOverflow.


Leave a Reply