Java Reference
In-Depth Information
the navigator object
The navigator object is another object that is a property of window and is available in all browsers.
Its name is more historical than descriptive. Perhaps a better name would be the “browser object,”
because the navigator object contains lots of information about the browser and the operating
system in which it's running.
Historically, the most common use of the navigator object is for handling browser differences.
Using its properties, you can find out which browser, version, and operating system the user
has. You can then act on that information and make sure your code works only in browsers
that support it. This is referred to as browser sniffing , and though it has its uses, it does have
limitations.
A better alternative to browser sniffing is feature detection , the act of determining if a browser
supports a particular feature. We won't go into these subjects here; later sections of this chapter are
devoted to browser sniffing and feature detection.
the geolocation Object
The HTML5 specification adds the geolocation property to navigator . Its purpose is simple: to
enable developers to obtain and use the position of the device or computer. That sounds like a scary
proposition, but users must give permission for that information to be retrieved and used.
At the heart of the geolocation object is its getCurrentPosition() method. When you call
this method, you must pass it a callback function , which is a function that executes when
getCurrentPosition() successfully completes its work. In Chapter 4, you learned that functions
are values. You can assign them to variables and pass them to other functions, and the latter is what
you do with the getCurrentPosition() method. For example:
function success(position) {
alert("I have you now!");
}
 
navigator.geolocation.getCurrentPosition(success);
In this code, success() is the callback function that executes when navigator.geolocation
.getCurrentPosition() determines the computer's or device's location. The parameter, position ,
is an object that contains the Earthly position and altitude of the computer or device, and you can
retrieve these pieces of information through its coords property, like this:
function success(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
var altitude = position.coords.altitude;
var speed = position.coords.speed;
}
The latitude , longitude , and altitude properties are self‐explanatory; they are simply numeric
values representing the latitude, longitude, and altitude of the device or computer, respectively.
The speed property retrieves the speed, or rather the velocity, of the device/computer in meters per
second.
 
Search WWH ::




Custom Search