Secure context
This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

The Geolocation.getCurrentPosition() method is used to get the current position of the device.

Syntax

navigator.geolocation.getCurrentPosition(success[, error[, [options]])

Parameters

success
A callback function that takes a Position object as its sole input parameter.
error Optional
An optional callback function that takes a PositionError object as its sole input parameter.
options Optional
An optional PositionOptions object.
Options inclues:
  • maximumAge: integer (milliseconds]) | infinity - maximum cached position age.
  • timeout: integer (milliseconds]) - amount of time before the error callback is invoked, if 0 it will never invoke.
  • enableHighAccuracy: false | true

Example

var options = {
  enableHighAccuracy: true,
  timeout: 5000,
  maximumAge: 0
};

function success(pos) {
  var crd = pos.coords;

  console.log('Your current position is:');
  console.log(`Latitude : ${crd.latitude}`);
  console.log(`Longitude: ${crd.longitude}`);
  console.log(`More or less ${crd.accuracy} meters.`);
}

function error(err) {
  console.warn(`ERROR(${err.code}): ${err.message}`);
}

navigator.geolocation.getCurrentPosition(success, error, options);

Specifications

Specification Status Comment
Geolocation API Recommendation Initial specification.

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
Basic supportChrome Full support 5Edge Full support YesFirefox Full support 3.5IE Full support 9Opera Full support 16
Full support 16
No support 10.6 — 15
Safari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support 15Safari iOS Full support YesSamsung Internet Android Full support Yes

Legend

Full support  
Full support

See also

Notes

In React native simulator, navigator.geolocation.getCurrentPosition() returns coordinates of San Francisco (37,-122)