The Geolocation API

Background

Introduction

With the emerging HTML5 standard, it is now possible to do many things from JavaScript in the web browser which previously could only be done within standalone applications. You have already seen one example in the canvas tag, which can be used to develop real-time 2D graphics applications in the browser. Today we will be looking at another, the Geolocation API (not strictly part of HTML5 but generally grouped with it)

The Geolocation API allows you to obtain the current location of the device running the browser from within JavaScript. Even on desktop browsers this will give a result if you are using a wireless network, but its real use is in the mobile web development world. A mobile browser can talk to the GPS chip of the phone and obtain the phone's current location on the earth. Alternatively, if the GPS chip is not available, a rough estimate can be obtained from cell towers or wireless networks.

Latitude and Longitude

In order to understand location-based applications, it is important to understand the coordinate system used on the earth. The most common coordinate system uses latitude and longitude. Latitude is a measure of how far north or south you are: the equator is at 0 degrees, while the North Pole is at 90 degrees North, we are at about 50 and Spain is at about 40. Longitude is a measure of how far east or west you are: 0 degrees of longitude is referred to as the Greenwich Meridian and passes through Greenwich, London. By contrast Germany is located between approximately 7 degrees and 15 degrees East, while New York is at 74 degrees West and the west coast of North America at approximately 120 degrees West.

Latitude and longitude

So a given point on the earth can be defined via its latitude and longitude. We are at, approximately, 50.9 North (latitude) and 1.4 West (longitude). By convention, latitudes north of the equator and longitudes east of Greenwich are treated as positive, so we can also define our position as longitude -1.4, latitude +50.9.

Using the Geolocation API

It is fairly straightforward to use the Geolocation API. Here is an example:



Geolocation Test



Geolocation Test

]]>

How is this working?

Watching the position

The above code will simply obtain the current position and stop. In a typical mobile GPS application, however, the user will want to be informed of their location on a regular basis. To do this we use watchPosition() in place of getCurrentPosition(). Here is an example:



Geolocation Test



Geolocation Test - Watching Position

]]>

Note that the code is almost the same as the first example, except:

Obtaining direction and speed of travel

As well as the position, a number of other properties are available to us. These include the direction we are heading (measured in degrees) and the speed of travel (measured in metres per second). To obtain these we use the heading and speed properties of the position object passed into our location-updating function (i.e. processPosition() in the above example). So in the example above we would reference the direction with pos.coords.heading and the speed with pos.coords.speed. Note that these are calculated from the previous GPS position.

Exercises

  1. Try out the first example above to see the Geolocation API working.
  2. You now have a choice between the following exercises.

Exercises requiring a smartphone

These exercises require a smartphone to test (e.g. iPhone 3.0+; Android 2.0+; recent Nokia smartphones). Also, because Edward is not externally accessible, you will not be able to test them on Edward. So, you will need to upload them to your own server space if you have it (for example, the PHPFog cloud space from last week)

Advanced exercises

For students comfortable with programming only!

  1. A pedometer is a device which keeps track of how far you have walked. Develop a simple pedometer using the Geolocation API. Every time a new location is received, calculate the distance travelled. The position object (the pos parameter to processPosition(), above) has an additional property timestamp (milliseconds since Jan 1st 1970). Save this in a global variable (i.e. a variable declared outside your functions) every time processPosition() runs. Then, next time processPosition() runs, we can calculate the time passed since the last call by subtracting the previous time from the current time. This then allows us to can calculate the distance as we have both the time passed and the average speed over that timespan. The distance in metres will be speed*(time/1000), with speed in m/s and time in milliseconds. Each time the location updates, add the distance travelled since the last update to a global variable and show that on the browser (in km)
  2. (Knowledge of basic trigonometry required) Using canvas, draw a graphical compass display, showing a circle containing a compass needle pointing in the current direction of movement.