Get Current location using HTML5 GeoLocation API

HTML5 GeoLocation API
The Geolocation API provides a method to locate the user’s exact (more or less – see below) position. This is useful in a number of ways ranging from providing a user with location specific information to providing route navigation. For privacy reasons, the user is asked for permission to report location information.


Browser compatibility

— Firefox 3.5+

— Internet Explorer 9+

— Chrome 9+

— Safari 5+

— Opera 10+

Functions used for location user


— Used to get current position of the device.


— This method will be called automatically each time the position of the device changes.


— This  method is used to unregister location/error monitoring handlers previously installed using navigator.geolocation.watchPosition()

Code to get user location using Geolocation API
var options = { 
     enableHighAccuracy: true, 
     timeout: 5000, 
     maximumAge: 0 
var success = function(position) { 
var loc = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); 
      marker = new google.maps.Marker({ 
      position: loc, 
      icon : ACC.config.commonResourcePath + "/images/map-logo.png", 
      title: "You are here" }); 
var error = function() { 
     output.innerHTML = "Unable to retrieve your location"; 
navigator.geolocation.getCurrentPosition(success, error, options); 
By using navigator.geolocation.getCurrentPosition function, we can retrive the user location. For privacy reasons, the user is asked for permission to report location information. This method has three parameters( Success, error, options).
Syntax is as follows

navigator.geolocation.getCurrentPosition(success[, error[, options]])



A callback function that takes a Position object as input parameter. Using this object we can fetch the latitude and longtitude of the current device.

error (Optional)

An optional callback function that takes a PositionError object input parameter. Using this we can find reason of error.


   1         Permission Denied

   2         Position Unavailable

   3         Time Out

options (Optional)

Containing option properties to pass as a parameter for navigator.geolocation.getCurrentPosition() and navigator.geolocation.watchPosition() functions