This page is Ready to Use

Notice: The WebPlatform project, supported by various stewards between 2012 and 2015, has been discontinued. This site is now available on github.

getCurrentPosition

Summary

Obtains the current location of the device.

Method of apis/geolocation/Geolocationapis/geolocation/Geolocation

Syntax

 Geolocation.getCurrentPosition(successCallback, errorCallback, options);

Parameters

successCallback

Data-type
any

The function to call when geographic position is successfully obtained. The function specified by the successCallback parameter takes one position parameter.

errorCallback

Data-type
any

(Optional)

The function to call when the method fails. The function specified by the errorCallback parameter takes one positionError parameter. To use the options parameter without using the errorCallback parameter, set the errorCallback parameter to null.

options

Data-type
any

(Optional)

JSON encoding

Return Value

No return value

Examples

Obtains user current position.

navigator.geolocation.getCurrentPosition(geoSuccess,geoError);

/* Position found*/
function geoSuccess(position)
{
alert('latitude: '+position.coords.latitude+' AND longitude: '+position.coords.longitude);
}

/* Position not found*/
function geoError(position)
{
alert("No position found");
}

Notes

Windows Internet Explorer 9. This property is supported only for webpages displayed in IE9 Standards mode. For more information, see Defining Document Compatibility. When the getCurrentPosition function is called, it initiates a request to acquire the geographic position, and then returns immediately. If the geographic position is successfully obtained, the callback function defined in the successCallback parameter is called. The position parameter of that function contains the data describing the current geographic location of the device running Internet Explorer. If the geographic location cannot be obtained and a callback function is specified as the errorCallback parameter, that function is called. The error parameter of the errorCallback function contains an error code indicating the reason for failure.

Note: The first time a web application calls the getCurrentPosition function, Internet Explorer requests permission to access the geographic location of the browser, unless the user has previously chosen to always allow or always deny permission for the website to determine location. If the user denies permission, the function declared by the errorCallback is called, and the code attribute of the error parameter of that function is set to PositionError.PERMISSION_DENIED. Support for the attributes of the options parameter depends on the location provider available to the device running Internet Explorer. There is no guarantee that changing the properties of these attributes will affect the results reported by the location provider.

Related specifications

W3C Geolocation Specification
W3C Editor’s Draft

Attributions

  • This article contains content originally from external sources, including ones licensed under the CC-BY-SA license. cc-by-sa-small-wpd.png

  • Portions of this content copyright 2012 Mozilla Contributors. This article contains work licensed under the Creative Commons Attribution-Sharealike License v2.5 or later. The original work is available at Mozilla Developer Network: Article

  • Microsoft Developer Network: Windows Internet Explorer API reference Article