O'Reilly logo

HTML5 and JavaScript Web Apps by Wesley Hales

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 7. Geolocation

The Geolocation API provides scripted access to geographical location information associated with the hosting device. This gives your applications the ability to locate users and track their latitude and longitude as they move about. This functionality could be used for many interesting use cases such as:

Geofencing

Give your app the ability to schedule a task to alert users the moment they enter or leave a location. You could also target ads for users within a certain city or state.

Geocoding

Combine your app with a service like the Google Maps API (Figure 7-1), and you can translate latitude and longitude coordinates into actual postal addresses.

General tracking

Track distances driven, walked, or ran.

The API itself is device agnostic; it doesn’t care how the browser determines location. The underlying mechanism to obtain the user’s actual location may be through WiFi, GPS, or by the user actually entering a zip code into the device. The API is designed to gather both “one-shot” position requests and repeated position updates. Of course, Geolocation is no different than any of the other HTML5e APIs in regard to bugs, workarounds, and differences in implementations across browsers. After a review of the basics, we’ll dive into the cross-browser nuances.

Using the Google Maps API with Geolocation

Figure 7-1. Using the Google Maps API with Geolocation

To access a user’s location, run the following JavaScript: ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required