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: Events, gmaps, Google Maps, hacks, Javascript | 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!