2012-11-06 15 views
5

プラットフォーム:iOS6/OSx Lion。Phonegap、Cordova watchposition fire成功1秒ごとに成功

私はPhonegap/Cordovaがnavigator.geolocation.watchPositionで動作する方法を困惑させようとしています。

maximumAge」というオプションは、システムに位置の取得を依頼するオプションです。これらのオプションで

ので:

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

私は位置要求が3秒ごとに発射されますespect?

そして、どんなに私は成功は1秒ごとに発射されて置く何maximumAge ...

誰でもしてください説明することができますか?

感謝さようなら
ロブ

+0

ここでは同じ問題です。 Cordova 2.2およびiOS 5.1/iOS6 iOSは1秒ごとに起動しますが、Androidは30日ごとに起動しますが、信頼性は高くありません。 – scald

+0

こんにちはニックハー、それはAndroidでも動作しないのですか?私が間違ったパラメタを使っていると思っていたのは本当に奇妙です。以前のバージョンのCordova/Phonegappでは "frequency"パラメータがありました。 ありがとうCiao Rob – oudoken

答えて

5

私は現在setIntervalgetCurrentPositionを使用することによって、この問題を回避働いています。結果がどうなるかは分かりませんが、これは私に最も支配力を与え、プラットフォーム間で最も一貫した方法と思われます。

// 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

Hi Scald、 お返事ありがとうございます。実際、関数watchPositionは、getCurrentPosition(私は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