geolocation.watchPosition

디바이스의 현재 위치에 대 한 변경 시계.

var watchId = navigator.geolocation.watchPosition(geolocationSuccess,
                                                  [geolocationError],
                                                  [geolocationOptions]);

매개 변수

  • geolocationSuccess: 현재의 위치를 전달 되는 콜백.

  • geolocationError: (선택 사항) 오류가 발생 하면 실행 되는 콜백.

  • geolocationOptions: (선택 사항)는 지리적 위치 옵션.

반환

  • 문자열: 시계 위치 간격을 참조 하는 시계 id를 반환 합니다. 시계 id와 함께 사용 해야 합니다 [geolocation.clearWatch](geolocation.clearWatch.html) 위치 변화에 대 한 보고 중지.

설명

geolocation.watchPosition비동기 함수가입니다. 그것은 위치에 변화를 탐지할 때 소자의 현재 위치를 반환 합니다. 새 위치를 검색 하는 장치는 [geolocationSuccess](parameters/geolocationSuccess.html) 콜백 실행 한 Position 매개 변수로 개체. 오류가 발생 하는 경우는 [geolocationError](parameters/geolocationError.html) 콜백 실행 한 [PositionError](PositionError/positionError.html) 매개 변수로 개체.

지원 되는 플랫폼

  • 안 드 로이드
  • 블랙베리 WebWorks (운영 체제 5.0와 더 높은)
  • iOS
  • Tizen
  • Windows Phone 7과 8
  • 윈도우 8

빠른 예제

// onSuccess Callback
//   This method accepts a `Position` object, which contains
//   the current GPS coordinates
//
function onSuccess(position) {
    var element = document.getElementById('geolocation');
    element.innerHTML = 'Latitude: '  + position.coords.latitude      + '<br />' +
                        'Longitude: ' + position.coords.longitude     + '<br />' +
                        '<hr />'      + element.innerHTML;
}

// onError Callback receives a PositionError object
//
function onError(error) {
    alert('code: '    + error.code    + '\n' +
          'message: ' + error.message + '\n');
}

// Options: throw an error if no update is received every 30 seconds.
//
var watchID = navigator.geolocation.watchPosition(onSuccess, onError, { timeout: 30000 });

전체 예제

<!DOCTYPE html>
<html>
  <head>
    <title>Device Properties Example</title>

    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
    <script type="text/javascript" charset="utf-8">

    // Wait for device API libraries to load
    //
    document.addEventListener("deviceready", onDeviceReady, false);

    var watchID = null;

    // device APIs are available
    //
    function onDeviceReady() {
        // Throw an error if no update is received every 30 seconds
        var options = { timeout: 30000 };
        watchID = navigator.geolocation.watchPosition(onSuccess, onError, options);
    }

    // onSuccess Geolocation
    //
    function onSuccess(position) {
        var element = document.getElementById('geolocation');
        element.innerHTML = 'Latitude: '  + position.coords.latitude      + '<br />' +
                            'Longitude: ' + position.coords.longitude     + '<br />' +
                            '<hr />'      + element.innerHTML;
    }

        // onError Callback receives a PositionError object
        //
        function onError(error) {
            alert('code: '    + error.code    + '\n' +
                  'message: ' + error.message + '\n');
        }

    </script>
  </head>
  <body>
    <p id="geolocation">Watching geolocation...</p>
  </body>
</html>