Geolocation

Geolocation is the art of figuring out where you are in the world and (optionally) sharing that information with people you trust. There are many ways to figure out where you are—your IP address, your wireless network connection, which cell tower your phone is talking to, or dedicated GPS hardware that receives latitude and longitude information from satellites in the sky.

Checking for geolocation support uses detection technique #1 (see Detection Techniques). If your browser supports the geolocation API, there will be a geolocation property on the global navigator object. If your browser doesn’t support the geolocation API, the geolocation property will be undefined. Here’s how to check for geolocation support:

function supports_geolocation() {
  return !!navigator.geolocation;
}

Instead of writing this function yourself, you can use Modernizr (see Modernizr: An HTML5 Detection Library) to detect support for the geolocation API:

if (Modernizr.geolocation) { // let's find out where you are! } else { // no native geolocation ...

Get HTML5: Up and Running 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.