HTML and CSS Reference
In-Depth Information
var speedEl = document.getElementById('speed');
navigator.geolocation.watchPosition(function (geodata) {
var speed = geodata.coords.speed;
if (speed === null || speed === 0) {
speedEl.innerHTML = “You're standing still!”;
} else {
// speed is in metres per second
// multiply by 2.23 to get miles per hour
speedEl.innerHTML = (speed * 2.23693629) + “Mph”;
}, function () {
speedEl.innerHTML = “Unable to determine speed :-(“;
}, { enableHighAccuracy: true });
In addition to the speed of the device, you can also get the alti-
tude, altitude accuracy, and heading. If these values aren't avail-
able—either because of the device or because the geolocation
can't get that particular piece of data—then the returned value
will be null .
Geo 404: the error handler
The second argument to the getCurrentPosition and watchPosition
methods is the error handler. This is particularly important if you
want to provide some alternative method of location (such as
manually) or you want to notify the user of any errors in getting
his position. The error handler may trigger if the user denies his
position, but it could be that the user has given you permission
and you are now watching his position on a mobile device and
the phone has gone out of reception. This too would cause the
error handler to trigger.
The error handler receives a single argument containing a posi-
tion error object with two properties:
readonly attribute unsigned short code
readonly attribute DOMString message
The code property will be one of the following:
PERMISSION_DENIED (numeric value 1)
POSITION_UNAVAILABLE (numeric value 2)
TIMEOUT (numeric value 3)
Search WWH ::

Custom Search