Tutorials Point


  HTML-5 Tutorial
  HTML5 Tag Reference
  HTML5 Useful References
  HTML5 Tools
  HTML5 Resources
  Selected Reading

Copyright © 2014 by tutorialspoint



  Home     References     Discussion Forums     About TP  

Geolocation getCurrentPosition() API


previous next AddThis Social Bookmark Button


Advertisements

Description:

The getCurrentPosition method retrieves 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.

Syntax:

Here is the syntax of this method:

getCurrentPosition(showLocation, ErrorHandler, options);

Parameters:

Here is the detail of parameters:

  • showLocation : This specifies the callback method that retrieves the location information. This method is called asynchronously with an object corresponding to the Position object which stores the returned location information.

  • ErrorHandler : This optional paramter specifies the callback method that is invoked when an error occurs in processing the asynchronous call. This method is called with the PositionError object that stores the returned error information.

  • options : This optional paramter specifies a set of options for retrieving the location information. You can specify (a) Accuracy of the returned location information (b) Timeout for retrieving the location information and (c) Use of cached location information .

Return value:

The getCurrentPosition method does not return a value.

Example:

<!DOCTYPE HTML>
<head>
<script type="text/javascript">
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 getLocation(){

   if(navigator.geolocation){
      // timeout at 60000 milliseconds (60 seconds)
      var options = {timeout:60000};
      navigator.geolocation.getCurrentPosition(showLocation, 
                                               errorHandler,
                                               options);
   }else{
      alert("Sorry, browser does not support geolocation!");
   }
}
</script>
</head>
<html>
<body>
   <form>
     <input type="button" onclick="getLocation();"  
                             value="Get Location"/>
   </form>
</body>
</html>

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



previous next Printer Friendly

Advertisements


  

Advertisements