Skip to content

Continuous Location

watchPosition() continuously monitors the user’s position and calls a callback whenever the position changes.

const watchId = navigator.geolocation.watchPosition(
(position) => {
console.log('Position updated:', position.coords);
// Update map, log movement, etc.
},
(error) => console.error(error),
{ enableHighAccuracy: true }
);
// Stop watching position updates
navigator.geolocation.clearWatch(watchId);
console.log('Stopped watching position');
  • Real-time location tracking (maps, delivery tracking)
  • Fitness apps (running routes, distance tracking)
  • Geofencing (trigger actions when entering/leaving areas)
  • watchPosition() — continuous position updates
  • Returns a watch ID for stopping updates
  • clearWatch(id) — stop watching
  • More battery-intensive than one-time requests