jQuery Google Map

Bakery map

Let’s work on the map first. To make thing easier, we’ll use a Google map jQuery plugin.

Here is the plugin page of the jQuery Google map: http://code.google.com/p/jquery-ui-map/

Download the file and put into the js folder.

In the #map page, we define a #map-element DIV in the content section.

1<div data-role='content'>
2  <h3>Where are we?</h3>
3  <p>Here is the map to the bakery.</p>
4  <div id='map-element'></div>
5</div>

Then we include the Google map API and the jQuery plugin JS file.

1<script src='http://maps.google.com/maps/api/js?sensor=true'></script>
2<script src='vendors/jquery.ui.map.js'></script>

There is no static content in the #map-element so it doesn’t have the width and height. We need to set it in CSS.

1#map-element {
2  width: 100%;
3  height: 400px;
4}

According to the plugin documentation, we can use the following syntax to initial the Google map with a marker on give GPS location.

1$('#map-element').gmap('addMarker', {'position': '22.191572,113.541553', 'bounds': true})

Where do we put initial the Google map?

We put it into the pageshow event.

1$(document).bind('pageshow', function(event, ui) {
2  if ( $(event.target).attr('id') === 'map' ) {
3    $('#map-element').gmap('addMarker', {'position': '22.191572,113.541553', 'bounds': true})
4  }
5});

If you are wondering how to obtain the geolocation, you can check this page about getting GPS location

Note that the page events happen for all pages changing. So we need to determine the page ID.

There are 4 page events that are useful for initializing and deconstructing logic during page changes.

  1. pagebeforeshow
  2. pageshow
  3. pagebeforehide
  4. pagehide

What’s next? We’re going to take a look at “News page”.

overlaied image when clicked on thumbnail

Makzan | Mobile web app dev with phonegap | Table of Content