코르도바-지리적 위치

위치 정보는 기기의 위도와 경도에 대한 정보를 얻는 데 사용됩니다.

1 단계-플러그인 설치

다음 코드를 입력하여이 플러그인을 설치할 수 있습니다. command prompt 창문.

C:\Users\username\Desktop\CordovaProject>cordova plugin add cordova-plugin-geolocation

2 단계-버튼 추가

이 튜토리얼에서는 현재 위치를 얻는 방법과 변경 사항을 확인하는 방법을 보여줍니다. 먼저 이러한 기능을 호출 할 버튼을 만들어야합니다.

<button id = "getPosition">CURRENT POSITION</button>
<button id = "watchPosition">WATCH POSITION</button>

3 단계-이벤트 리스너 추가

이제 장치가 준비되면 이벤트 리스너를 추가하려고합니다. 아래 코드 샘플을 추가하여onDeviceReady 기능 index.js.

document.getElementById("getPosition").addEventListener("click", getPosition);
document.getElementById("watchPosition").addEventListener("click", watchPosition);

3 단계-함수 생성

두 개의 이벤트 리스너에 대해 두 개의 함수를 생성해야합니다. 하나는 현재 위치를 얻는 데 사용되고 다른 하나는 위치를 보는 데 사용됩니다.

function getPosition() {
   var options = {
      enableHighAccuracy: true,
      maximumAge: 3600000
   }
   var watchID = navigator.geolocation.getCurrentPosition(onSuccess, onError, options);

   function onSuccess(position) {
      alert('Latitude: '          + position.coords.latitude          + '\n' +
         'Longitude: '         + position.coords.longitude         + '\n' +
         'Altitude: '          + position.coords.altitude          + '\n' +
         'Accuracy: '          + position.coords.accuracy          + '\n' +
         'Altitude Accuracy: ' + position.coords.altitudeAccuracy  + '\n' +
         'Heading: '           + position.coords.heading           + '\n' +
         'Speed: '             + position.coords.speed             + '\n' +
         'Timestamp: '         + position.timestamp                + '\n');
   };

   function onError(error) {
      alert('code: '    + error.code    + '\n' + 'message: ' + error.message + '\n');
   }
}

function watchPosition() {
   var options = {
      maximumAge: 3600000,
      timeout: 3000,
      enableHighAccuracy: true,
   }
   var watchID = navigator.geolocation.watchPosition(onSuccess, onError, options);

   function onSuccess(position) {
      alert('Latitude: '          + position.coords.latitude          + '\n' +
         'Longitude: '         + position.coords.longitude         + '\n' +
         'Altitude: '          + position.coords.altitude          + '\n' +
         'Accuracy: '          + position.coords.accuracy          + '\n' +
         'Altitude Accuracy: ' + position.coords.altitudeAccuracy  + '\n' +
         'Heading: '           + position.coords.heading           + '\n' +
         'Speed: '             + position.coords.speed             + '\n' +
         'Timestamp: '         + position.timestamp                + '\n');
   };

   function onError(error) {
      alert('code: '    + error.code    + '\n' +'message: ' + error.message + '\n');
   }
}

위의 예에서는 두 가지 방법을 사용합니다. getCurrentPositionwatchPosition. 두 기능 모두 세 가지 매개 변수를 사용합니다. 클릭하면CURRENT POSITION 버튼을 누르면 경고에 지리적 위치 값이 표시됩니다.

클릭하면 WATCH POSITION버튼을 누르면 동일한 경고가 3 초마다 트리거됩니다. 이렇게하면 사용자 기기의 움직임 변화를 추적 할 수 있습니다.

노트

이 플러그인은 GPS를 사용하고 있습니다. 때때로 시간에 값을 반환 할 수없고 요청이 시간 초과 오류를 반환합니다. 이것이 우리가 지정한 이유입니다enableHighAccuracy: truemaximumAge: 3600000.즉, 요청이 제 시간에 완료되지 않으면 마지막으로 알려진 값을 대신 사용합니다. 이 예에서는 maximumAge를 3600000 밀리 초로 설정합니다.