2012-11-06 5 views
5

플랫폼 : iOS6/OSx Lion.Phonegap, Cordova watchposition 성공 1 초마다

나는 Phonegap/Cordova가 navigator.geolocation.watchPosition과 작동하는 방식을 고민하고 있습니다.

"maximumAge"옵션은 시스템에 위치 검색을 요청하는 옵션입니다. 이 옵션

그래서 :

{ maximumAge: 3000, timeout: 5000, enableHighAccuracy: true } 

나는 위치 요청이 매 3 초마다 해고 될 것 espect?

와 상관없이 내가 성공이 1 초마다 해고 무엇을 넣어 maximumAge ...

누구나하시기 바랍니다 설명 할 수 있습니까?

나는 현재 setIntervalgetCurrentPosition를 사용하여이 문제를 해결하고 있어요

+0

여기에서 같은 문제가 발생합니다. Cordova 2.2 및 iOS 5.1/iOS6. iOS가 1 초마다 작동하지만 Android는 30 분마다 작동하는 것으로 보이지만 안정적이지는 않습니다. – scald

+0

안녕하세요 nickhar, 안드로이드에서도 작동하지 않습니까? 그것은 정말 이상한 생각이 나는 잘못된 매개 변수를 사용하고, 이전 버전의 Cordova/Phonegapp "주파수"매개 변수가있었습니다. 감사합니다 Ciao Rob – oudoken

답변

5

감사합니다 안녕
롭. 나는 그 결과가 무엇인지 모르겠다. 그러나 이것은 나에게 가장 많은 통제력을 주며, 플랫폼 전반에 걸쳐 가장 일관된 방법 인 것으로 보인다.

// call this once 
setupWatch(3000); 

// sets up the interval at the specified frequency 
function setupWatch(freq) { 
    // global var here so it can be cleared on logout (or whenever). 
    activeWatch = setInterval(watchLocation, freq); 
} 

// this is what gets called on the interval. 
function watchLocation() { 
    var gcp = navigator.geolocation.getCurrentPosition(
      updateUserLoc, onLocationError, { 
       enableHighAccuracy: true 
      }); 


    // console.log(gcp); 

} 

// do something with the results 

function updateUserLoc(position) { 


var location = { 
    lat : position.coords.latitude, 
    lng : position.coords.longitude 
}; 

console.log(location.lat); 
console.log(location.lng); 
} 

// stop watching 

function logout() { 
    clearInterval(activeWatch); 
} 
+0

안녕하세요 Scald, 답장을 보내 주셔서 감사합니다. 사실 watchPosition 함수는 getCurrentPosition (timer로)을 호출하는 것일뿐입니다 (cordova.js 내부를 살펴 봤습니다). Btw 코드가 매우 유용합니다! "clearInterval (activeWatch)"로 타이머를 정지 시키면 "watchPosition"도 중지됩니까? 많은 시간을 "setupWatch"라고 부르면 어쩌면 "watchPosition"이있는 타이머가 많이 생깁니 까? 고마워 Ciao Rob – oudoken

+0

Rob - 내 코드에서 watchPosition이 사용되지 않습니다. 나는 표면 코드 바 아래 어딘가 비슷한 일을하고 있을지도 모른다는 것을 이해한다. 그러나 이것은'watchPosition' 호출보다 훨씬 잘 작동한다. 예,'clearInterval' 호출은'watchLocation'을 멈추고'setupWatch'에 대한 후속 호출은 새로운 타이머를 생성 할 수 있도록 그 타이머를 클리어합니다. – scald

+0

안녕하세요. Scald, 고마워요. 당신 코드를 시험해 보겠습니다. 그게 cordova입니다 .js는 iOS에 몇 가지 문제가 있습니다. 고마워요! Ciao Rob – oudoken

관련 문제