Hands on: Geolocation using HTML5

Learn how to use HTML5 to plot points on a map and determine current position

1 2 3 4 5 6 7 8 9 Page 5
Page 5 of 9

After successfully determining the position, the variable pos of the success callback in the so-called Position interface contains coordinate data (pos.coords) plus a timestamp in milliseconds since 1970 (pos.timestamp).

Geographic position output in Mozilla Firefox
Geographic position output in Mozilla Firefox

In addition to latitude, longitude, and altitude, pos.coords also includes information about the accuracy of the position (accuracy, altitudeAccuracy) plus possible speed and direction (heading). Whereas Google Chrome is limited to the attributes required in the specification, Firefox (here, version 3.6) outputs quite a lot of additional information -- even address details, showing an extract of the result of JSON.stringify(pos):

Extract from the result of JSON.stringify(pos) for Firefox 3.6

{"coords": <br> ..// ...<br>   "address": <br>     {"streetNumber":"6","street":"Postgasse", <br>      "premises":null,"city":"Murnau am Staffelsee", <br>      "county":"Garmisch-Partenkirchen","region":"Bavaria", <br>      "country":"Germany","countryCode":"DE", <br>      "postalCode":"82418","contractID":"", <br>      "classDescription":"wifi geo position address object", <br>      // ...<br>     }, <br>   // ...<br> }

The browser offers a remarkable amount of information! Where it all comes from will be explained in the following section.

Technical Background of Determining Position

If you access the website http://www.google.com from abroad, you may be surprised to find that you are automatically redirected to the relevant Google domain of the country you are in. This works even if your browser is not geolocation capable: Google uses a simple trick and locates your whereabouts via the IP address. Browsers supporting the Geolocation API can achieve a significantly greater accuracy by making use of other technical options. The following four methods are currently in use:

  1. In PCs with wired Internet connections, the position is located via the IP address. This way of determining position is rather inaccurate, as you would expect.
  2. The position can be determined much more precisely if there is a wireless LAN connection. Google has collected data worldwide from public and private WLANs.
  3. If the hardware has a mobile communications chip (for example, in a smartphone), it tries to calculate the position within the mobile communications network.
  4. If the hardware also has a GPS sensor, the position can be determined even more accurately. GPS is a satellite-based positioning system and can achieve accuracy to the meter range even with cheap sensors, provided the conditions are favorable (outside of buildings, unobstructed horizon, etc.).

Only the GPS sensor works offline; methods 1-3 require Internet access and are implemented through a server location service. These services are available from Google (Google Location Service, used in Firefox, Chrome, and Opera) and another American company, Skyhook Wireless (used in Safari and early versions of Opera). But how do the service providers get the location information of wireless and mobile networks? In parallel with the photos taken by Google for the service Street View, the Google Street View vehicles also save information on public and private WLANs. The revelation, in spring 2010, that these vehicles collected not only the MAC address and SSID of the WLAN, but also user data, shed a bad light on Google, resulting in several public apologies.

1 2 3 4 5 6 7 8 9 Page 5
Page 5 of 9
7 inconvenient truths about the hybrid work trend
Shop Tech Products at Amazon