Geolocation API basics

The Geolocation API asks the browser for a position estimate; the browser and user decide whether the request may proceed.

What you will learn

Requesting a position

navigator.geolocation.getCurrentPosition(
  (position) => {
    const { latitude, longitude, accuracy } = position.coords;
    console.log(latitude, longitude, accuracy);
  },
  (error) => console.warn(error.code, error.message)
);

The coordinates are an estimate. The accuracy value is expressed in meters and does not mean that the result is exact.

Permission is part of the feature

Explain what the user gains before requesting location. A denial, timeout, or unavailable position is a normal outcome, not an exceptional one.

Common mistakes