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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.