Interactive Mapping Blog

Mapping Solutions News

Archive for the ‘HTML5’ Category

Bing Maps AJAX & HTML5 GeoLocation API

Thursday, June 23rd, 2011

In the second in our series on mobile mapping with Bing Maps AJAX V7 we are going to show you one of the great features of HTML5 that you can start using right now on mobile devices. Where the desktop may take years before all users have access to the features of HTML5 the vast majority of smartphone users already have HTML5 compatible browsers.

HTML5 GeoLocation API

HTML5’s geolocation API allows you to request a users location by prompting the user to optionally share their location information with your website. It is currently supported on:

image

With WP7 hopefully coming soon. Rather than repeat the entire documentation for the geolocation API lets show you how to use it in a code example. Here is an example JavaScript method we have written that we can call to get the user location.

  1. function GetLocation() {
  2.     //check if location api supported
  3.     if (!!navigator.geolocation) {
  4.         
  5.         //request current location
  6.         navigator.geolocation.getCurrentPosition(UpdateLocation,HandleErrors);
  7.     }
  8. }

 

Notice the line “!!navigator.geolocation” this is checking that the browser supports the geolocation api, you should of course in a real application show some kind of message if they do not. Once we have checked if the browser has support we simply call the “navigator.geolocation.getCurrentPosition” method passing in two method names (we will write those in a moment) the first to handle the location response and the second to handle any errors.

The first of the two methods which handles a valid location response looks like this:

  1. function UpdateLocation(position) {
  2.     var latitude = position.coords.latitude;
  3.     var longitude = position.coords.longitude;
  4.  
  5.     //move map to new location and zoom in
  6.     map.setView({ center: { latitude: latitude, longitude: longitude }, zoom: 16, mapTypeId: Microsoft.Maps.MapTypeId.aerial });
  7. }

As you can see it accepts a position parameter from which we can get the user latitude and longitude. We the set our Bing Map to the new location, and in this example zoom the map in on that location.

The next method handles any errors that might occur, this is a very basic example which you would want to expand upon in production:

  1. function HandleErrors(error) {
  2.     //handle geolocation errors and alert user
  3.     switch (error.code) {
  4.         case error.PERMISSION_DENIED: alert("user did not share geolocation data");
  5.             break;
  6.  
  7.         case error.POSITION_UNAVAILABLE: alert("could not detect current position");
  8.             break;
  9.  
  10.         case error.TIMEOUT: alert("retrieving position timed out");
  11.             break;
  12.  
  13.         default: alert("unknown error");
  14.             break;
  15.     }  
  16. }

As you can see it shows a relevant JavaScript alert informing the user of what the error was. Note that one of these errors is simply that the user has not decided to share their location with you website.

Finally here is the entire complete code so you can see how these methods are called, and how the basic Bing Map is setup for mobile display:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html>
  3. <head>
  4.     <title>Geolocation default</title>
  5.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6.     <meta name="viewport" content="width=device-width, minimum-scale=1, maximum-scale=1" />
  7.     <script type="text/javascript" src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0"></script>
  8.     <script type="text/javascript">
  9.         var map = null;
  10.  
  11.         function GetMap() {
  12.             map = new Microsoft.Maps.Map(document.getElementById('myMap'), { credentials: 'YOURBINGMAPSKEY' });
  13.             GetLocation();
  14.         }
  15.  
  16.         function GetLocation() {
  17.             //check if location api supported
  18.             if (!!navigator.geolocation) {
  19.                 
  20.                 //request current location
  21.                 navigator.geolocation.getCurrentPosition(UpdateLocation,HandleErrors);
  22.             }
  23.         }
  24.  
  25.         function UpdateLocation(position) {
  26.             var latitude = position.coords.latitude;
  27.             var longitude = position.coords.longitude;
  28.  
  29.             //move map to new location and zoom in
  30.             map.setView({ center: { latitude: latitude, longitude: longitude }, zoom: 16, mapTypeId: Microsoft.Maps.MapTypeId.aerial });
  31.         }
  32.  
  33.         function HandleErrors(error) {
  34.             //handle geolocation errors and alert user
  35.             switch (error.code) {
  36.                 case error.PERMISSION_DENIED: alert("user did not share geolocation data");
  37.                     break;
  38.  
  39.                 case error.POSITION_UNAVAILABLE: alert("could not detect current position");
  40.                     break;
  41.  
  42.                 case error.TIMEOUT: alert("retrieving position timed out");
  43.                     break;
  44.  
  45.                 default: alert("unknown error");
  46.                     break;
  47.             }  
  48.         }
  49.  
  50.     </script>
  51.     
  52. </head>
  53. <body onload="GetMap();" style="padding:0;margin:0;">
  54.  
  55.     <div id="myMap" style="position: absolute; width: 100%; height: 100%;">
  56.     </div>
  57.  
  58. </body>
  59. </html>

You can see a live demo at http://bit.ly/j6dT0S which will work on your mobile device, or HTML5 enabled desktop browser.

Creating mobile map infoboxes with Bing Maps and JQuery Mobile

Tuesday, June 21st, 2011

This is the first in our new series on mobile mapping and are based on a talk I gave recently at the Bing Maps user group about some tips and techniques for creating maps specifically for mobile devices.

This first tutorial will focus on creating Infoboxes (or popups) specifically targeted for mobile devices using a combination of Bing Maps Ajax V7 and Jquery Mobile.

If you want to skip the overview and get straight into the code click here.

Fitting everything in

One of the first challenges you encounter when developing for mobile devices is how to fit all your information into such a small amount of space. If we look at the default Bing Maps infobox you can see it fits nicely on the ipad, but not so nicely on the iphone:

image

image

Giving users what they expect

Even if we could find a way to fit our information into such a small space on the iphone using the default infobox would not give users a great experience, and certainly not one they are used to getting from native applications on their mobile.

A better approach to this is to see how we can mimic other user interface concepts that users are already familiar with and use these to display the information that is associated with our pushpins.

The approach we are going to demonstrate in the rest of this tutorial is to create a typical mobile dialog for our pushpin using the Jquery Mobile library:

image

The Code

We will start by setting up a basic html page, adding the script and css references for Bing Maps and Jquery Mobile:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html>
  3. <head>
  4.     <title>Infobox default</title>
  5.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6.     <script type="text/javascript" src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0"></script>
  7.     <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0b1/jquery.mobile-1.0b1.min.css" />
  8.     <script src="http://code.jquery.com/jquery-1.6.1.min.js"></script>
  9.     <script src="http://code.jquery.com/mobile/1.0b1/jquery.mobile-1.0b1.min.js"></script>
  10. </head>
  11. <body>
  12.  
  13. </body>
  14. </html>

 

As you can see this is a pretty basic page and so far it does nothing. Now we need to add a few bits of markup so that Jquery Mobile helps us setup a basic mobile ready page, including automatic ajax page loading. We start by adding the following meta tag to the head:

<meta name="viewport" content="width=device-width, minimum-scale=1, maximum-scale=1" />

Now we need to add some html in the body that will tell Jquery Mobile that we have a mobile page (each html file can contain multiple pages in Jquery Mobile), add the basic map div ready for us to load a Bing map into:

  1. <body class="ui-mobile-viewport">
  2.     <div data-role="page">
  3.         <div id="myMap" style="position: absolute; width: 100%; height: 100%;">
  4.         </div>
  5.     </div>
  6. </body>

Jquery Mobile is very much about convention over configuration so the html 5 style “data-role” and the “ui-mobile-viewport” class on the body tag are used by Jquery Mobile to work its magic and apply its ‘mobile-esq’ css styling.

Finally we are going to add the javascript code (in the head section) we need to both load the Bing Map and fire a Jquery Mobile method to load a new page when we click on the puhspin:

  1. <script type="text/javascript">
  2.     var map = null, pushpinClick;
  3.  
  4.     function GetMap() {
  5.         map = new Microsoft.Maps.Map(document.getElementById('myMap'), { credentials: 'YOURBINGMAPSKEY' });
  6.         var pushpin = new Microsoft.Maps.Pushpin(map.getCenter(), null);
  7.         map.entities.push(pushpin);
  8.         pushpinClick = Microsoft.Maps.Events.addHandler(pushpin, 'click', AddMobileInfobox);
  9.     }
  10.  
  11.     function AddMobileInfobox() {
  12.         $.mobile.changePage("infoboxcontent.html", "slideup");
  13.     }
  14. </script>

The code above shows the normal way of creating a Bing Map and then adding a pushpin with a click event that calls the “AddMobileInfobox” method. Inside this click handler is the only piece of JavaScript we need to write specifically for Jquery Mobile. It calls the $.mobile.changepage method passing in the name of the html page we want to load and the name of the animation to see (see here for more animation options). This method loads the new page using ajax and presents it over the map.

The only thing left to do is to create the “infoboxcontent.html” page itself, this again uses Jquery Mobile to provide the styling:

  1. <!DOCTYPE html>
  2. <html class="ui-mobile landscape undefined min-width-320px min-width-480px min-width-768px min-width-1024px">
  3. <head>
  4.     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5.     <meta name="viewport" content="width=device-width, minimum-scale=1, maximum-scale=1" />
  6.     <title>jQuery Mobile Framework – Dialog Example</title>
  7.     <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0b1/jquery.mobile-1.0b1.min.css" />
  8.     <script src="http://code.jquery.com/jquery-1.6.1.min.js"></script>
  9.     <script src="http://code.jquery.com/mobile/1.0b1/jquery.mobile-1.0b1.min.js"></script>
  10. </head>
  11. <body class="ui-mobile-viewport">
  12.     <div data-role="page" data-url="" class="ui-page ui-body-c ui-page-active">
  13.         <div data-role="content" data-theme="a" class="ui-content ui-body-a" role="main">
  14.             <h3>Infobox Title</h3>
  15.             <p>
  16.                 Infobox description content can go here and fit on a mobile screen just fine.</p>
  17.             <a href="#" data-role="button" data-rel="dialog" data-transition="slidedown" data-theme="b"
  18.                 class="ui-btn ui-btn-corner-all ui-shadow ui-btn-up-b"><span class="ui-btn-inner ui-btn-corner-all">
  19.                     <span class="ui-btn-text">Full Details</span></span></a>
  20.             <a href="#" data-role="button"
  21.                 data-rel="back" data-theme="a" class="ui-btn ui-btn-corner-all ui-shadow ui-btn-up-a">
  22.                 <span class="ui-btn-inner ui-btn-corner-all"><span class="ui-btn-text">Cancel</span></span></a>
  23.         </div>
  24.     </div>
  25.     <div class="ui-loader ui-body-a ui-corner-all" style="top: 100px;">
  26.         <span class="ui-icon ui-icon-loading spin"></span>
  27.         <h1>
  28.             loading</h1>
  29.     </div>
  30. </body>
  31. </html>

Again this uses the same page markup as before to tell Jquery Mobile how to style the page and this time it has some more new Jquery Mobile tags and styles that automatically turn our hyperlinks into mobile styled buttons. As you can see this page has zero custom javascript and all the styling is automatically applied by Jquery Mobile. When you load the page it looks like this:

image

Clicking the cancel button closes the dialog and takes us back to the map without reloading the page, all by the magic of Jquery Mobile.

Summary

Hopefully this tutorial has shown you a really quick and easy way to produce infoboxes for mobile devices with very little code mostly thanks to Jquery Mobile. Over the coming weeks we will share a few other mobile mapping tutorials so let us know what issues you would like to see covered.

You can view a live demo and view the code at http://bit.ly/jexeja