Copyright ©

Geolocation watchPosition() API

previous next



The watchPosition method retrieves periodic updates about the current geographic location of the device. The location is expressed as a set of geographic coordinates together with information about heading and speed.

The location information is returned in a Position object. Each update returns a new Position object.


Here is the syntax of this method:

watchPosition(showLocation, ErrorHandler, options);


Here is the detail of parameters:

Return value:

The watchPosition method returns a unique transaction ID (number) associated with the asynchronous call. Use this ID to cancel the watchPosition call and to stop receiving location updates.


<script type="text/javascript">
var watchID;
var geoLoc;

function showLocation(position) {
  var latitude = position.coords.latitude;
  var longitude = position.coords.longitude;
  alert("Latitude : " + latitude + " Longitude: " + longitude);

function errorHandler(err) {
  if(err.code == 1) {
    alert("Error: Access is denied!");
  }else if( err.code == 2) {
    alert("Error: Position is unavailable!");
function getLocationUpdate(){

      // timeout at 60000 milliseconds (60 seconds)
      var options = {timeout:60000};
      geoLoc = navigator.geolocation;
      watchID = geoLoc.watchPosition(showLocation, 
      alert("Sorry, browser does not support geolocation!");
     <input type="button" onclick="getLocationUpdate();"  
                             value="Watch Update"/>

To learn above concept - Do Online Practice using latest version of either Opera or Firefox.

previous next

Copyright ©