Java Reference
In-Depth Information
If you need to retrieve more than one of these values, it makes sense to assign position.coords to a
variable and then use the variable to retrieve the positional values. For example:
function success(position) {
var crds = position.coords;
 
var latitude = crds.latitude;
var longitude = crds.longitude;
var altitude = crds.altitude;
var speed = crds.speed;
}
This reduces the amount of code you have to type. It also has the advantage of reducing the size of
your code, resulting in a slightly faster download time.
The getCurrentPosition() method accepts a second parameter, another callback function that
executes when an error occurs:
function geoError(errorObj) {
alert("Uh oh, something went wrong");
}
 
navigator.geolocation.getCurrentPosition(success, geoError);
The error callback function has a single parameter that represents the reason for
getCurrentPosition() is failure. It is an object containing two properties. The first, code , is a numeric
value indicating the reason of failure. The following table lists the possible values and their meanings:
Value
desCription
Failure occurred because the page did not have permission to acquire the position of
the device/computer.
1
An internal error occurred.
2
The time allowed to retrieve the device's/computer's position was reached before
the position was obtained.
3
The second property is called message ; it's a human‐readable message that describes the error.
trY it out Using Geolocation
In this example, you use the geolocation object to retrieve the latitude and longitude of the device/
computer:
<!DOCTYPE html>
 
<html lang="en">
<head>
<title>Chapter 8, Example 1</title>
</head>
<body>
<script>
function geoSuccess(position) {
Search WWH ::




Custom Search