Chapter 28APIs: The Geolocation API

The Geolocation API provides an easy way to retrieve the exact position of your users. For example, you can create an app that gives personalized suggestions to the users based on their current location. You may also plot their position on the map to show navigation details.

In this chapter, I will give you an overview of the Geolocation API and show how you can use it to create magical location-based HTML5 apps.

Hitting the Surface

Before using the API, let’s just make sure the browser supports it:

if (navigator.geolocation) {
  // do something awesome
}
else {
  // provide alternative content
}

The same check can be achieved through Modernizr:

if (Modernizr.geolocation) { //do something awesome } else { //provide ...

Get Jump Start HTML5 now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.