2014-12-13 1 views
0

지리적 위치 확인 모바일 애플리케이션 (경로 추적)이 있으며 사용자가 수정할 수있는 위치 업데이트를위한 맞춤 시간 간격을 지정할 수 있어야합니다. 정확성이 중요하고 getCurrentPosition()이 제대로 작동하지 않는 것처럼 watchPosition() 함수를 사용하고 있습니다. 그러나 사용자가 자신의 선호도에 따라 업데이트 간격을 사용자 지정할 수 있기를 바랍니다.지리적 위치 맞춤 업데이트 간격 (30 초)

여기 내 코드의 대략적인 표현은 http://jsfiddle.net/ppyordanov/taz4g5t0/5/입니다.

HTML :

<div id="result"></div> 

JS :

var latitude, longitude, accuracy; 
function setGeolocation() { 
    var geolocation = window.navigator.geolocation.watchPosition( 
     function (position) { 
      latitude = position.coords.latitude; 
      longitude = position.coords.longitude; 
      accuracy = position.coords.accuracy; 
      document.getElementById('result').innerHTML += 
        'lat: ' + latitude + ', ' 
       + 'lng: ' + longitude + ', ' 
       + 'accuracy: ' + accuracy + '<br />'; 
     }, function() { 
      /*error*/ 
     }, { 
      maximumAge: 250, 
      enableHighAccuracy: true 
     } 
    ); 

}; 

setGeolocation(); 

window.setTimeout(function() { 
     setGeolocation(); 
    }, 
    30000 //check every 30 seconds 
); 

I()는 시간 간격을 조작 할 수 있지만,이 출력에 영향을 보이지 않는다에서는 setTimeout을 사용할 수있다. 매초마다 한 번씩 계속 업데이트됩니다. 어떻게 30 초로 늘릴 수 있습니까?

답변

1

대신 setGeolocation(); 모든 n초를 호출, 나는 window.navigator.geolocation.watchPosition() 전화의 위치를 ​​저장 한 다음 setInterval() 기능을 통해 별도의 기능으로 업데이트합니다.