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.