2016-07-21 6 views
0

を動作していない中で、ユーザーの位置を取得し、私は次のコードをしている:ons.readyは

ons.ready(function() { 

    navigator.geolocation.getCurrentPosition(
     function(position) { 
      geo.lat = position.coords.latitude; 
      geo.lon = position.coords.longitude; 
     } 
    ); 
}); 
時には

ではなく、私は次のエラーを取得、すべての時間:私は、ユーザーの位置を

Location access is not available. 
Error in Error callbackId: Geolocation54410059 

が必要私のアプリのメインページにデータを読み込む。これを行うための最良の場所はどこですか?

答えて

0

ons.ready domがロードされているときにコードポイントプラグインを使用して、地理位置を取得しています。それで、コードワが準備される前にそれを使うことはできません。

だけで実行します。

document.addEventListener('deviceready', function() { 
    // now cordova is ready 
    navigator.geolocation.getCurrentPosition(function(position) { 
      geo.lat = position.coords.latitude; 
      geo.lon = position.coords.longitude; 
    }); 
}, false); 

そして、私が考える罰金になります。

編集:あなたの関数にそれらのオプションを追加すること

試してみてください。

navigator.geolocation.getCurrentPosition(function(position) { 
       geo.lat = position.coords.latitude; 
       geo.lon = position.coords.longitude; 
     }, { maximumAge: 3000, timeout: 5000, enableHighAccuracy: true }); 

あなたが「enableHighAccuracy」と「maximumAgeという」の好きな値に置くことができますが、あなたはので、「タイムアウト」オプションを提供しなければなりませんアンドロイドにいくつかの欠点があります:

Android Quirks

If Geolocation service is turned off the onError callback is invoked after timeout interval (if specified). If timeout parameter is not specified then no callback is called.

関連する問題