Quiz Entry - updated: 2026.07.14
How do you continuously track the user's position with the Geolocation API?
Call watchPosition() to receive continuous position updates as the device moves, and pass the watch ID it returns to clearWatch() to stop.
let watchId;
function startTracking() {
watchId = navigator.geolocation.watchPosition(showPosition);
}
function stopTracking() {
navigator.geolocation.clearWatch(watchId);
}
function showPosition(position) {
console.log(position.coords.latitude, position.coords.longitude);
}
Key differences from getCurrentPosition:
| Method | Behavior |
|---|---|
getCurrentPosition() |
One-time position query |
watchPosition() |
Continuous updates on movement |
Important:
watchPosition()returns an ID to identify the watch- Pass this ID to
clearWatch()to stop tracking - Callback fires whenever position changes significantly