Friday, February 7, 2014

HTML Geolocation API





Make your applications smarter by leveraging the power of the Geolocation API!

The Geolocation API is used to determine a user's geographical position. Since this can compromise a user's privacy they must give permission before your HTML application can gain access to a users location information.


Here's how Google's Chrome browser presents the Geolocation permission dialog.


Once the user has allowed it, the application can then make one of two different calls into the Geolocation API. The API will call back into your application when the position information is available.

  • navigator.geolocation.getCurrentPosition(showPosition,showError)
    • Produces a one time callback providing location information 
  • navigator.geolocation.watchPosition(showPosition,showError)
    • Produces continual callbacks as the users location changes

The video (above) starts with a slide deck introducing some of the key concepts, then it demonstrates the Geolocation capability within the browser and finally shows the JavaScript code.

Here's a link to the demo page where you can test it yourself and download the code if you like.

This is Part1 of a series of articles showcasing some of the new HTML5 application features. Follow-on articles will cover the App Cache which allows your applications to run offline, as well as Web Storage (local and session), an expansion on the cookie storage concept and finally I'll cover Web Workers which allow you to spin up separate threads in the browser to do work in the background freeing up the UI thread, making your applications much more responsive when doing real work.
I'm including the slide deck separately here in case you would like to go through it on your own.





No comments:

Post a Comment