MOBILE

Programming the Mobile Web : Geolocation and Maps - Showing a Map

2/11/2011 9:26:39 AM
Once we have located the user (via a client or server solution), we may want to display a map showing the user’s position, and/or a list of points of interest or other information superposed on the map.

To do this, we should use one of the available public maps APIs: Google Maps, Bing Maps from Microsoft, Yahoo! Maps, or OVI Maps from Nokia. However, if we analyze mobile compatibility, there is really only one choice: Google Maps. Compatibility for the others may increase in the future, but at present Google’s API is by far the best supported.

There are actually two Google APIs that are useful for mobile browsers: the Google Maps API v3 and the Google Maps Static API. The first one is the same service that we can find in any website using Google Maps. However, it is currently compatible only with iPhone and Android devices; on other devices, this API will not work properly. The Static API will allow us to show a static map compatible with any mobile browser.


Note:

Yahoo! APIs will be compatible if we use the Point of Interest (POI) search or even the geocoding services. Microsoft also offers Bing Maps Web Services with similar solutions.


1. Google Maps API v3

If we are sure that the device is an iPhone or Android device, we should use the Google Maps API version 3, as shown in Figure 1.

Figure 1. Here we can see the Google Maps API on an iPhone showing the user’s current location using the multiplatform framework.


To use the API, we first need to include the script loader:

<script type="text/javascript"
src="http://maps.google.com/maps/api/js?sensor=true">

The sensor value must be true if the device has geolocation support via the operating system (this is the case on both iPhone and Android devices). We should check whether the device is compatible either on the server.

Then, we need to create a div tag in our HTML and define its dimensions as 100%:

function showMap() {
var useragent = navigator.userAgent;
var divMap = document.getElementById("map");

if (useragent.indexOf('iPhone') != −1 || useragent.indexOf('Android') != −1 ||
useragent.indexOf(iPod) != −1 ) {
divMap.style.width = '100%';
divMap.style.height = '100%';
// ...
} else {
// Google Maps not compatible with this mobile device
}
}



Warning:

We should use the Google Maps API only for iPhone or Android devices. On other devices, this will not work and we will need to use the Google Maps Static API instead.


The other requirement for iPhone and Android from version 1.5 is to define the meta tag to work without user zooming and with an initial scale of 1.0. This is necessary to avoid usability problems with the map zooming. So, in the head we should add:

<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />

A full sample looks like this:

<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<script type="text/javascript"
src="http://maps.google.com/maps/api/js?sensor=true"></script>
<script type="text/javascript">
function init() {
var useragent = navigator.userAgent;
var divMap = document.getElementById("map");

if (useragent.indexOf('iPhone') != −1 ||
useragent.indexOf('Android') != −1 ||
useragent.indexOf('iPod') != −1 ) {
divMap.style.width = '100%';
divMap.style.height = '100%';
position = getPosition(); // This needs to be implemented
var latlng = new google.maps.LatLng(position.latitude,
position.longitude);
var options = {
zoom: 7,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("divMap"),
options);
} else {
// Google Maps not compatible with this mobile device
}
}

</script>
</head>
<body onload="init()">
<div id="divMap"></div>
</body>
</html>


To see the full Google Maps API documentation, go to http://code.google.com/apis/maps/documentation/v3.

2. Google Maps Static API

If we need compatibility with all mobile devices, even the ones without JavaScript support, we can use the free Google Maps Static API, which allows us to show a map as a static image without any automatic interaction.


Warning:

To use this API you will need to sign up for a free API key at http://code.google.com/apis/maps/signup.html.


This API is very simple and doesn’t require any JavaScript or server code. We will use it inside an HTML image tag, in the source URL. The URL will look like this:

http://maps.google.com/maps/api/staticmap?parameters

To use this API we need to get the location from the server or the client and generate the image URL dynamically using JavaScript on compatible devices. We need to remember that we may not want to show the user’s location, but rather a map of some other place.


Note:

With the Google Maps Static API we can show a map on any mobile phone on the market, even those without JavaScript or Ajax support.


The common parameters include:

sensor

Must be true for a mobile device.

center

May be a position using latitude,longitude, or a city name.

zoom

The level of zoom required, from 0 (world view) to 21 (building view).

size

The size in pixels of the image required (e.g., 220×300). We should get the device’s screen size from the server or from JavaScript.

format

Accepts GIF, JPEG, or PNG. The default is PNG, which is suitable for mobile devices.

mobile

Marking this parameter as true creates different rendering images optimized for viewing on mobile devices. Google suggests using false in the case of iPhone or Android devices.


Note:

The API is more complex, and it can even show marks and routes over the map. To see the full documentation, go to http://code.google.com/apis/maps/documentation/staticmaps/.


For example, if we are using PHP and we just have the latitude and longitude (acquired by any method), we should use:

<?php
// $latitude and $longitude already acquired
$url = "http://maps.google.com/maps/api/staticmap?center=$latitude,
$longitude&zoom=14&size=220x300&sensor=true&key=API_KEY";
?>
<img src="<?php echo $url ?>" width="220" height="300" />

3. Following LBS

Location-based services are great. We can create very useful applications combining maps, places (Points of Interest), the user’s location, and even other users’ locations. Some good books about LBS and geographical services are available for marketers and developers. I encourage you to look more deeply into this topic, so you can create better mobile web experiences.

Other  
  •  Mobile Application Security - BlackBerry Security - Permissions and User Controls (part 2)
  •  Mobile Application Security - BlackBerry Security - Permissions and User Controls (part 1) - RIM Controlled APIs
  •  Windows Phone 7 Development : Working with Controls and Themes - Introducing the Metro Design System
  •  Windows Phone 7 Development : WebBrowser Control - Saving Web Pages Locally
  •  Programming the Mobile Web : Geolocation and Maps - Detecting the Location (part 3)
  •  Programming the Mobile Web : Geolocation and Maps - Detecting the Location (part 2) - Google Gears
  •  Programming the Mobile Web : Geolocation and Maps - Detecting the Location (part 1) - W3C Geolocation API
  •  Programming the Mobile Web : Geolocation and Maps - Location Techniques
  •  iPhone Programming : Table-View-Based Applications - Connecting the Controller to the Model
  •  Programming the Mobile Web : Mobilizing WordPress and Other CMSs
  •  Programming the Mobile Web : Server-Side Browser Detection and Content Delivery - Content Adaptation
  •  Programming the Mobile Web : Multimedia and Streaming
  •  Mobile Application Security : BlackBerry Security - Development and Security Testing
  •  Mobile Application Security : BlackBerry Security - Introduction to Platform
  •  Windows Phone 7 Development : Using a WebBrowser Control to Display Dynamic Content
  •  Windows Phone 7 Development : Using a WebBrowser Control to Display Local HTML Content
  •  Windows Mobile Security - Networking
  •  Windows Mobile Security - Local Data Storage
  •  Windows Mobile Security - Permissions and User Controls
  •  Windows Phone 7 Development : Using a WebBrowser Control to Display Web Content
  •  
    Video
    Top 10
    Windows Server 2003 : Domain Name System - Command-Line Utilities
    Microsoft .NET : Design Principles and Patterns - From Principles to Patterns (part 2)
    Microsoft .NET : Design Principles and Patterns - From Principles to Patterns (part 1)
    Brother MFC-J4510DW - An Innovative All-In-One A3 Printer
    Computer Planet I7 Extreme Gaming PC
    All We Need To Know About Green Computing (Part 4)
    All We Need To Know About Green Computing (Part 3)
    All We Need To Know About Green Computing (Part 2)
    All We Need To Know About Green Computing (Part 1)
    Master Black-White Copying
    Most View
    Epic Moments in Sports (Part 1)
    iPhone 3D Programming : Blending and Augmented Reality - Blending Recipe
    HP X2301 : Micro Thin, Macro Sights
    Advanced ASP.NET : Understanding Caching
    Amateur Astronomy Applications (Part 1) - WorldWide Telescope, Stellarium
    How To Buy…SSD Drives (Part 2)
    Macro Marvel by Peiling Lee
    Under The Surface (Part 1)
    Microsoft .NET : Design Principles and Patterns - Object-Oriented Design (part 1) - Basic OOD Principles
    Hosting a Multi-Tenant Application on Windows Azure : Selecting a Single-Tenant or Multi-Tenant Architecture
    Google Nexus 4 - Features Of A High-End Smartphone For Half The Price
    Developing an SEO-Friendly Website: Content Delivery and Search Spider Control (part 2)
    SQL Server 2008 : Explaining Advanced Query Techniques - Managing Internationalization Considerations
    Frequently Asked Questions About UAC
    Windows Server 2008 : Harnessing the Power and Potential of FIM
    Parallel Programming with Microsoft .Net : Dynamic Task Parallelism - An Example
    Microsoft XNA Game Studio 3.0 : Displaying Images - Using Resources in a Game (part 3) - Sprite Drawing with SpriteBatch
    Windows Server 2003 : Active Directory - Understanding Operations Master Roles
    Windows 7 : Using Windows Defender (part 3) - Using Windows Defender Tools & Troubleshooting Windows Defender
    iOS 6 Beta Review (Part 1)