Geolocation: clearWatch() メソッド

安全なコンテキスト用: この機能は一部またはすべての対応しているブラウザーにおいて、安全なコンテキスト (HTTPS) でのみ利用できます。

Geolocation.clearWatch() メソッドは、以前 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);

仕様書

Specification
Geolocation API
# clearwatch-method

ブラウザーの互換性

BCD tables only load in the browser

関連情報