Book Image

Instant HTML5 Geolocation How-to

By : Benjamin Otto Werdmulle
Book Image

Instant HTML5 Geolocation How-to

By: Benjamin Otto Werdmulle

Overview of this book

We don't just surf the Web from our desktops any more – we take it with us, everywhere we go. Modern devices contain sophisticated hardware and software to determine the user's location. Apps such as Foursquare and Google Maps use this to create new kinds of functionality. Now, you can do this too with the HTML5 Geolocation API. "Instant HTML5 Geolocation How-to" is a simple guide to adding location information to your web applications. The practical, easy-to-follow recipes are designed to help you learn the ins and outs of the API. You'll learn how to use it, how it works, and how to save and display geographic information on the web. Beginning with a solid grounding in how the Geolocation API works and when to use it, you will learn how to determine, store, display, and track the user's location via a series of clear recipes. You will learn the different ways location is determined on different devices, including desktops and laptops that don't have GPS units. You'll also learn how to selectively use these different behaviours, based on the speed, accuracy, and battery life requirements of your application. You'll also get some hints about using MySQL databases to store sets of location data. "Instant HTML5 Geolocation How-to" will teach you everything you need to know about retrieving the location information your application needs, across multiple devices and platforms.
Table of Contents (7 chapters)


Welcome to Instant HTML5 Geolocation How-to. This guide will help you make any web application aware of a user's location, quickly and easily. You will learn how to map a user's geographic location, integrate with popular mapping platforms, and track the user over time. In the process, you will create a simple application that generates a KML file containing both a path and points that you choose to save along that path (which can then be imported into Google Maps).

The iPhone changed the way we use the web. It was the first mobile device that included web browsing as a primary function. Suddenly, with mobile data and a phone like the iPhone, the web could be in your pocket, wherever you went. Through HTML5 and APIs, web applications could be truly context sensitive; they could know where you were, if you wanted them to, and react accordingly.

The Geolocation API is not part of HTML5, and is, instead, a JavaScript API standard. However, it sits alongside the evolving HTML5 specification as an important part of any modern web development toolkit.

What this book covers

Understanding the Geolocation API (Simple) discusses how the JavaScript Geolocation API works, when you should use it, and how it is implemented in different browsers and on different devices.

Setting up the application (Simple) explains how to configure and set up a sample application layout suitable for running a simple Geolocation-aware web application. Each step is explained in a way that will help you understand how your data will be stored so that you can scale your application when you're ready.

Getting the user's location (Intermidiate) illustrates how to detect Geolocation support in the user's web browser, use the API to obtain the user's coordinates, and handle any errors that might occur.

Displaying the user's location using the Google Maps API (Intermidiate) discusses how to display the coordinates obtained in the previous section in a visual way using Google Maps.

Displaying the user's location using a KML feed (Intermidiate) explains how to create the KML feed from coordinates you have saved in previous sections, and test it by importing it into Google Maps. KML feeds are supported by popular mapping applications such as Google Maps and Google Earth, as well as specialist GIS software.

Tracking and updating the user's location (Intermidiate) illustrates how to track the user and show his/her path, using simple web technologies. Sometimes, tracking the user's movements is important. Here we will use watchPosition() and clearPosition() to track the user's location over time, and change the maps that were created in the previous section.

What you need for this book

You need to have access to a server or computer running Apache Web Server, MySQL, and PHP. You will also need a text editor, a recent version of a major web browser, and optionally, a web-capable mobile device with GPS support.

Who this book is for

Instant HTML5 Geolocation How-to is for web developers who want to incorporate Geolocation technology into their applications. There is no need to know anything about Geolocation software or standards; each section has been designed to help you understand how the API works from beginning to end. However, this book assumes a general understanding of web technologies, and both JavaScript and jQuery in particular.


In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text are shown as follows: "In this section, we will be concentrating on index.php and callback.php."

A block of code is set as follows:


$server = '';      // Enter your database server here
$username = '';      // Enter your database username here
$password = '';      // Enter your database password here
$database = '';     // Enter your database name here

// Connect to the database

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "Select File | Save in your web browser while viewing the feed."


Warnings or important notes appear in a box like this.


Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a book that you need and would like to see us publish, please send us a note in the SUGGEST A TITLE form on or e-mail .

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.


Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from


Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.


You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.