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:
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");
// do something with geolocation
To get the current location, use the getCurrentLocation() function, which takes three parameters:
A callback function that is executed when the call is successful.
An error callback function that is called when an error occurs.
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
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:
(specified in degrees)
(specified in degrees)
(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.)
(specified in meters)
(specified in meters)
Search WWH ::

Custom Search