--- title: Geolocation.watchPosition() slug: Web/API/Geolocation/watchPosition translation_of: Web/API/Geolocation/watchPosition ---
{{ APIref("Geolocation API") }}
Geolocation.watchPosition() 這個方法是用來註冊一個處理的函式,當使用者的裝置位置更新時,這個函式所傳入的回呼函式(callback function) 就會自動被呼叫。你也可以選擇性的定義錯誤時哪些錯誤回呼函式(error callback function) 需要被呼叫。
這個函式將回傳一組 ID 編號,此編號搭配 {{domxref("Geolocation.clearWatch()")}} 函式,即可停止更新使用者的位置。
id = navigator.geolocation.watchPosition(success[, error[, options]])
var id, target, options;
function success(pos) {
var crd = pos.coords;
if (target.latitude === crd.latitude && target.longitude === crd.longitude) {
console.log('Congratulations, you reached the target');
navigator.geolocation.clearWatch(id);
}
}
function error(err) {
console.warn('ERROR(' + err.code + '): ' + err.message);
}
target = {
latitude : 0,
longitude: 0
};
options = {
enableHighAccuracy: false,
timeout: 5000,
maximumAge: 0
};
id = navigator.geolocation.watchPosition(success, error, options);
如果你的應用程式是跑在 firefox OS上,請參考 geolocation wake lock,此方法可以讓你的程式在背景或螢幕關上時也能持續收到位置更新。
| Specification | Status | Comment |
|---|---|---|
| {{SpecName('Geolocation', '#watch-position', 'Geolocation.watchPosition()')}} | {{Spec2('Geolocation')}} | Initial specification. |