Menu

HTML5 TUTORIALS - HTML5 Geolocation

HTML5 Geolocation

ADVERTISEMENTS


var geolocation = navigator.geolocation;

ADVERTISEMENTS

MethodDescription
getCurrentPosition()

This method retrieves the current geographic location of the user.

watchPosition()

This method retrieves periodic updates about the current geographic location of the device.

clearWatch()

This method cancels an ongoing watchPosition call.

ADVERTISEMENTS

Example:


function getLocation() {
   var geolocation = navigator.geolocation;
   geolocation.getCurrentPosition(showLocation, errorHandler);
}

PropertyTypeDescription
coordsobjects

Specifies the geographic location of the device. The location is expressed as a set of geographic coordinates together with information about heading and speed.

coords.latitudeNumber

Specifies the latitude estimate in decimal degrees. The value range is [-90.00, +90.00].

coords.longitudeNumber

Specifies the longitude estimate in decimal degrees. The value range is [-180.00, +180.00].

coords.altitudeNumber

[Optional] Specifies the altitude estimate in meters above the WGS 84 ellipsoid.

coords.accuracyNumber

[Optional] Specifies the accuracy of the latitude and longitude estimates in meters.

coords.altitudeAccuracyNumber

[Optional] Specifies the accuracy of the altitude estimate in meters.

coords.headingNumber

[Optional] Specifies the device's current direction of movement in degrees counting clockwise relative to true north.

coords.speedNumber

[Optional] Specifies the device's current ground speed in meters per second.

timestampdate

Specifies the time when the location information was retrieved and the Position object created.

Example:


function showLocation( position ) {
  var latitude = position.coords.latitude;
  var longitude = position.coords.longitude;
  ...
}

PropertyTypeDescription
codeNumber

Contains a numeric code for the error.

messageString

Contains a human-readable description of the error.

CodeConstantDescription
0UNKNOWN_ERROR

The method failed to retrieve the location of the device due to an unknown error.

1PERMISSION_DENIED

The method failed to retrieve the location of the device because the application does not have permission to use the Location Service.

2POSITION_UNAVAILABLE

The location of the device could not be determined.

3TIMEOUT

The method was unable to retrieve the location information within the specified maximum timeout interval.

Example:


function errorHandler( err ) {
  if (err.code == 1) {
    // access is denied
  }
  ...
}


getCurrentPosition(callback, ErrorCallback, options)

PropertyTypeDescription
enableHighAccuracyBoolean

Specifies whether the widget wants to receive the most accurate location estimate possible. By default this is false.

timeoutNumber

The timeout property is the number of milliseconds your web application is willing to wait for a position.

maximumAgeNumber

Specifies the expiry time in milliseconds for cached location information.

Example:


function getLocation() {
   var geolocation = navigator.geolocation;
   geolocation.getCurrentPosition(showLocation, errorHandler,
                                 {maximumAge: 75000});
}