Sunday, June 12, 2011

Get details of PositionError object

PositionError object defined at W3C Geolocation API, it hold properties of code and message of the error.

The code attribute must return the appropriate code from the following list:
  • PERMISSION_DENIED (numeric value 1)
    The location acquisition process failed because the application origin does not have permission to use the Geolocation API.
  • POSITION_UNAVAILABLE (numeric value 2)
    The position of the device could not be determined. For instance, one or more of the location providers used in the location acquisition process reported an internal error that caused the process to fail entirely.
  • TIMEOUT (numeric value 3)
    The length of time specified by the timeout property has elapsed before the implementation could successfully acquire a new Position object.

The message attribute must return an error message describing the details of the error encountered. This attribute is primarily intended for debugging and developers should not use it directly in their application user interface.

example:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
if (window.navigator.geolocation) {
navigator.geolocation.getCurrentPosition(successCallback, errorCallback);
} else {
alert("Sorry! your browser does not support Geolocation API.");
}

function successCallback(position){
var msg ="";

msg += "Latitude: " + position.coords.latitude + "\n";
msg += "Longitude: " + position.coords.longitude + "\n";
msg += "Accuracy: " + position.coords.accuracy;

alert(msg);
}

function errorCallback(error) {
switch (error.code) {
case error.PERMISSION_DENIED:
alert("PERMISSION DENIED: " + error.message);
break;
case error.POSITION_UNAVAILABLE:
alert("POSITION UNAVAILABLE: " + error.message);
break;
case error.TIMEOUT:
alert("TIMEOUT: " + error.message);
break;
}
}

</script>
<meta charset="UTF-8">
<title>Simple Geolocation API demo</title>
</head>
<body>
<h1>Simple Geolocation API demo</h1>


</body>
</html>


output:
details of PositionError object

No comments:

Post a Comment