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
- How a position watch differs from one request
- How to use the returned watch ID
- Why stopping the watch protects privacy and battery life
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
- Provide a visible Stop button for a tracking feature.
- Stop on page or component cleanup when your framework has a lifecycle.
- Explain what is collected and when the watch ends.
- Use the lowest accuracy and update frequency that still solves the problem.