此頁面由社群從英文翻譯而來。了解更多並加入 MDN Web Docs 社群。

View in English Always switch to English

Geolocation:watchPosition() 方法

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨2015年7月⁩.

Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

Geolocation 介面的 watchPosition() 方法用於註冊一個處理函式,該函式會在裝置位置每次變更時自動被呼叫。你也可以選擇性地指定一個錯誤處理回呼函式。

請注意,除了需要安全上下文之外,此功能也可能被 geolocation Permissions-Policy 阻擋,並且還需要使用者明確授予權限。如有需要,當呼叫此方法時,將會提示使用者。權限狀態可以使用權限 API 中的 geolocation 使用者權限來查詢。

語法

js
watchPosition(success)
watchPosition(success, error)
watchPosition(success, error, options)

參數

success

一個回呼函式,它接受一個 GeolocationPosition 物件作為輸入參數。

error 選擇性

一個可選的回呼函式,它接受一個 GeolocationPositionError 物件作為輸入參數。

options 選擇性

一個可選的物件,為位置監控提供組態選項。有關可能選項的更多詳細訊息,參見 Geolocation.getCurrentPosition()

回傳值

一個整數 ID,用於識別已註冊的處理函式。此 ID 可以傳遞給 Geolocation.clearWatch() 以取消註冊該處理函式。

範例

js
let id;
let target;
let options;

function success(pos) {
  const crd = pos.coords;

  if (target.latitude === crd.latitude && target.longitude === crd.longitude) {
    console.log("恭喜,你已到達目標");
    navigator.geolocation.clearWatch(id);
  }
}

function error(err) {
  console.error(`錯誤(${err.code}):${err.message}`);
}

target = {
  latitude: 0,
  longitude: 0,
};

options = {
  enableHighAccuracy: false,
  timeout: 5000,
  maximumAge: 0,
};

id = navigator.geolocation.watchPosition(success, error, options);

規範

Specification
Geolocation
# watchposition-method

瀏覽器相容性

參見