v157 · approximate geolocation
Watch, switch, and revoke
The parts that bite. PositionOptions is read once when the watch starts, so you cannot change accuracyMode on a live watch — you have to clearWatch() and start again. Permission can be revoked from under you while the watch runs. And a browser that has not shipped the option drops it without a word.
Run a watch
This asks for your real location and keeps asking. A watch stays open until you stop it. Coordinates never leave the page — only the accuracy radius is displayed.
- Watch id
- none
- Updates
- 0
- Last accuracy
- —
- Permission
- unknown
Event log
- Nothing yet.
code path
// Options are read when the watch is created. Changing your mind means
// tearing the watch down and building a new one.
let id = navigator.geolocation.watchPosition(onFix, onError, {
accuracyMode: "approximate",
timeout: 15000,
});
function switchTo(mode) {
navigator.geolocation.clearWatch(id);
id = navigator.geolocation.watchPosition(onFix, onError, { accuracyMode: mode });
}
// Permission can go away while the watch is open. Listen, do not poll.
const status = await navigator.permissions.query({ name: "geolocation" });
status.addEventListener("change", () => console.log(status.state));