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

浏览器兼容性

另见