← Lessons

quiz vs the machine

Gold1340

Frontend

The Geolocation API

Read the user's position once or watch it as they move.

4 min read · core · beat Gold to climb

Locating the device

The Geolocation API exposes the device position through navigator geolocation. It is permission gated and works best over HTTPS.

  • getCurrentPosition asks for the location once
  • watchPosition delivers updates as the device moves
  • clearWatch stops an active watch to save battery
  • Both take success and error callbacks

The position object

A success callback receives a position with coords, containing latitude, longitude, and an accuracy in metres. It may also include altitude, heading, and speed depending on hardware.

Options and permissions

You can request enableHighAccuracy, set a timeout, and accept a cached reading with maximumAge. The user must consent, and they can deny or revoke permission at any time, so always handle the error callback.

Key idea

The Geolocation API reads device position via getCurrentPosition or watchPosition, is permission gated, and always needs an error path for denial.

Check yourself

Answer to earn rating on the learn ladder.

1. Which method delivers continuous location updates as a device moves?

2. Why must you always provide an error callback for geolocation?