HTML and CSS Reference
In-Depth Information
Using the Geolocation Object
The geolocation API is provided by the geolocation object, which you can access through the navigator
object like this:
navigator.geolocation
If a falsy value is returned such as null or undefined, then geolocation is not supported on the current
browser. You can check for support using code like this:
if (!navigator.geolocation) {
alert("Geolocation is not supported");
}
else
// do something with geolocation
To get the current location, use the getCurrentLocation() function, which takes three parameters:
1.
A callback function that is executed when the call is successful.
2.
An error callback function that is called when an error occurs.
3.
A PositionOptions collection that contains zero or more options.
The last two parameters can be omitted. The following options are supported:
maximumAge - the browser can cache previous positions and return this without actually
trying to determine the location. However, the maximumAge attribute specifies how long
(in milliseconds) a previous position can be reused without re-querying the current
location.
timeout - the timeout attribute specifies how long the browser should wait for a response
from the geolocation object. This is also expressed in milliseconds.
enableHighAccuracy - this is just a hint to the browser. If you don't need greater accuracy
for a particular purpose, setting this to false may yield a faster response or use less power,
which is a consideration for mobile devices.
If the call was successful, the position is passed to the callback function that was specified. The Position
object includes a coords object that contains the following required properties:
latitude
(specified in degrees)
longitude
(specified in degrees)
accuracy
(specified in meters)
In addition, the following optional properties may be provided depending on the environment and the
available hardware. If these are not supported they will be set to null. (The optional properties are typically
available only when GPS is used.)
altitude
(specified in meters)
altitudeAccuracy
(specified in meters)
 
Search WWH ::




Custom Search