HTML5 Geolocation: Problems and Solutions
Join the DZone community and get the full member experience.
Join For FreeJavaScript can ask browsers lots of questions, and IP addresses can (sort of) localize clients -- but only the new Geolocation API provides a standard language for browsers to tell webpages where the browser is physically located, in any way the browser chooses.
The concept of geolocation is simple ('where are you?'); but the W3C spec explicitly specifies that the API be 'agnostic to the underlying sources of location information' because the methods required to gather that information can become quite complex (GPS, cell triangulation, IP address, whatever), and can vary with device and circumstance
So browser manufacturers need to think hard about how to implement the Geolocation API. But, fortunately for web developers, the W3C's agnosticism requirement means that web apps are thoroughly abstracted from the actual geodata. This is particularly important because plenty of location-specific web apps might be used on multiple devices, from many places (think Foursquare, or Yelp), depending on whether the user is currently in place p or deciding whether (and how) to go there.
Recently .net magazine posted an extensive set of problems and solutions using the Geolocation API, excerpted from Chapter 8 of the HTML5 Cookbook by Christopher Schmitt and Kyle Simpson. If you're thinking about using the Geolocation API, this is an excellent place to start.
The excerpt/article begins with the basics ('getting basic geolocation data'), but includes many suggestions for practical use (for instance, IP-to-location fallback in JavaScript, if the browser doesn't support geolocation), useful related APIs (e.g., lat-long (what the Geolocation API returns) to Google Maps, and vice versa), directions from current location (also using Google -- though there are lots more APIs you might prefer for certain usecases, and more.
Extremely useful collection of libraries, APIs, snippets, and suggestions for anyone writing geolocation into a web app. Check it out here.
Opinions expressed by DZone contributors are their own.
Comments