Geolocation: clearWatch() 方法

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

安全上下文: 此功能僅在安全上下文(HTTPS)中可用,且支援此功能的瀏覽器數量有限。

clearWatch() 方法是 Geolocation 介面的一部分,用於登出先前透過 Geolocation.watchPosition() 安裝的位置/錯誤監控處理程式。

語法

js
clearWatch(id)

引數

id

要移除的處理程式是在安裝時由 Geolocation.watchPosition() 方法返回的 ID 號。

返回值

無(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("Congratulations, you've reached the target!");
    navigator.geolocation.clearWatch(id);
  }
}

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

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

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

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

規範

規範
Geolocation
# clearwatch-method

瀏覽器相容性

另見