Geolocation:clearWatch() 方法

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

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

Geolocation 介面的 clearWatch() 方法用於取消註冊先前使用 Geolocation.watchPosition() 安裝的位置/錯誤監控處理器。

語法

js
clearWatch(id)

參數

id

你想移除的處理器的 ID 編號,其是由 Geolocation.watchPosition() 方法在安裝時所回傳的。

回傳值

無(undefined)。

範例

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
# clearwatch-method

瀏覽器相容性

參見