--- title: Geolocation.watchPosition() slug: Web/API/Geolocation/watchPosition tags: - API - Geolocation - Geolocation API - Method - Reference - Secure context translation_of: Web/API/Geolocation/watchPosition ---
{{domxref("Geolocation")}} 인터페이스의 watchPosition() 메서드는 장치의 위치가 바뀔 때마다 자동으로 호출할 처리기 함수를 등록할 때 사용합니다. 선택적으로 오류 발생 시 사용할 콜백 함수도 지정할 수 있습니다.
navigator.geolocation.watchPosition(success[, error[, options]])
successerror {{optional_inline}}options {{optional_inline}}등록한 처리기를 식별할 때 사용하는 정수 ID. ID를 {{domxref("Geolocation.clearWatch()")}}에 전달해 등록을 해제할 수 있습니다.
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 | Status | Comment |
|---|---|---|
| {{SpecName('Geolocation', '#dom-geolocation-watchposition', 'watchPosition()')}} | {{Spec2('Geolocation')}} | Initial specification. |
{{Compat("api.Geolocation.watchPosition")}}