Geolocation getCurrentPosition() Method

Definition and Usage

getCurrentPosition() The method returns the current position of the device.

Example

Get the longitude and latitude of the user's location:

var x = document.getElementById("demo");
function getLocation() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPosition);
  } else {
    x.innerHTML = "This browser does not support geolocation.";
  }
}
function showPosition(position) {
  x.innerHTML = "Latitude: " + position.coords.latitude +
  "<br>Longitude: " + position.coords.longitude;
}

Try it yourself

Syntax

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

Parameters

Parameters Description
success Required. A callback function that takes a Position object as its only input parameter.
error Optional. A callback function that takes a PositionError object as its only input parameter.
options Optional. PositionOptions Object.

Return Value

The current location of the device.

Browser Support

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
5.0 9.0 3.5 5.0 16.0