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 July 2015.

보안 컨텍스트: 이 기능은 일부 또는 모든 지원 브라우저보안 컨텍스트 (HTTPS)에서만 사용할 수 있습니다.

Geolocation 인터페이스의 watchPosition() 메서드는 장치의 위치가 바뀔 때마다 자동으로 호출할 처리기 함수를 등록할 때 사용합니다. 선택적으로 오류 발생 시 사용할 콜백 함수도 지정할 수 있습니다.

구문

js
navigator.geolocation.watchPosition(success[, error[, options]])

매개변수

success

GeolocationPosition 객체를 입력 매개변수로 사용하는 콜백 함수.

error Optional

GeolocationPositionError 객체를 입력 매개변수로 사용하는 콜백 함수.

options Optional

위치 감지 설정 옵션을 제공하는 PositionOptions 객체.

반환 값

등록한 처리기를 식별할 때 사용하는 정수 ID. ID를 Geolocation.clearWatch()에 전달해 등록을 해제할 수 있습니다.

예제

js
var id, target, options;

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

  if (target.latitude === crd.latitude && target.longitude === crd.longitude) {
    console.log("Congratulations, you reached the target");
    navigator.geolocation.clearWatch(id);
  }
}

function error(err) {
  console.warn("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

브라우저 호환성

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
watchPosition

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

같이 보기