Handling Errors

This topic provides a sample that demonstrates how to implement a callback function to handle errors that result from calls to the Geolocation API. This error callback function is specified using the optional second parameter to getCurrentPosition or watchPosition.

The error callback function takes one PositionError parameter. The PositionError object provides a code attribute that indicates the type of error and a message attribute that may contain an error message. The message attribute is not intended for end-user consumption and should be used for debugging only.

The following table describes the possible types of errors.

Error Description
PositionError.PERMISSION_DENIED (1) This site does not have permission to use the Geolocation API.
PositionError.POSITION_UNAVAILABLE (2) The current position could not be determined.
PositionError.TIMEOUT (3) The current position could not be determined within the timeout specified.

The following example shows how to call getCurrentPosition with both a success callback and an error callback as parameters. The error callback function displays a message based on the error object’s code attribute.

<!DOCTYPE html>  
<title>Geolocation API Example</title>
<meta http-equiv="X-UA-Compatible" content="IE=9" />
<script type="text/javascript">

function setText(val, e) {
    document.getElementById(e).value = val;

function insertText(val, e) {
    document.getElementById(e).value += val;

var nav = null; 

function requestPosition() {
  if (nav == null) {
      nav = window.navigator;
  if (nav != null) {
      var geoloc = nav.geolocation;
      if (geoloc != null) {
          geoloc.getCurrentPosition(successCallback, errorCallback);
      else {
          alert("Geolocation not supported");
  else {
      alert("Navigator not found");

function successCallback(position)
   setText(position.coords.latitude, "latitude");
   setText(position.coords.longitude, "longitude");

function errorCallback(error) {
    var message = "";  

    // Check for known errors
    switch (error.code) {
      case error.PERMISSION_DENIED:
          message = "This website does not have permission to use " + 
                    "the Geolocation API";
       case error.POSITION_UNAVAILABLE:
          message = "The current position could not be determined.";
       case error.TIMEOUT:
          message = "The current position could not be determined " + 
                    "within the specified timeout period.";            

    // If it is an unknown error, build a message that includes 
    // information that helps identify the situation so that 
    // the error handler can be updated.
    if (message == "")
        var strErrorCode = error.code.toString();
        message = "The position could not be determined due to " + 
                  "an unknown error (Code: " + strErrorCode + ").";


<label for="latitude">Latitude: </label><input id="latitude" /> <br />
<label for="longitude">Longitude: </label><input id="longitude" /> <br />
<input type="button" onclick="requestPosition()" value="Get Latitude and Longitude"  /> 


Getting the Current Location

How to Create a Location-Aware Webpage

Specifying the Time-out Duration

Watching for Location Changes