Secure context
This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
Geolocation
인터페이스의 watchPosition()
메서드는 장치의 위치가 바뀔 때마다 자동으로 호출할 처리기 함수를 등록할 때 사용합니다. 선택적으로 오류 발생 시 사용할 콜백 함수도 지정할 수 있습니다.
구문
navigator.geolocation.watchPosition(success[, error[, options]])
매개변수
success
GeolocationPosition
객체를 입력 매개변수로 사용하는 콜백 함수.error
OptionalGeolocationPositionError
객체를 입력 매개변수로 사용하는 콜백 함수.options
Optional- 위치 감지 설정 옵션을 제공하는
PositionOptions
객체.
반환 값
등록한 처리기를 식별할 때 사용하는 정수 ID. ID를 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 |
---|---|---|
Geolocation API The definition of 'watchPosition()' in that specification. |
Recommendation | Initial specification. |
브라우저 호환성
BCD tables only load in the browser
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
같이 보기
- Geolocation API 사용하기
- 메서드가 속한
Geolocation
인터페이스와, 접근하는 방법인NavigatorGeolocation.geolocation
. - 처리기를 해제하는
Geolocation.clearWatch()
메서드 - 비슷한
Geolocation.getCurrentPosition()
메서드