Watch a changing position

Use watchPosition() when a feature needs updates as the device moves. Always keep the returned ID so you can stop the watch later.

What you will learn

const watchId = navigator.geolocation.watchPosition(
  position => console.log(position.coords),
  error => console.warn(error.code, error.message),
  { enableHighAccuracy: false, timeout: 10000, maximumAge: 30000 }
);

// Stop when the feature closes or the user turns it off.
navigator.geolocation.clearWatch(watchId);

watchPosition() calls the success callback when a new position is available. It does not automatically stop when the page no longer needs updates.

Design the stop action