2011-11-21 40 views
8

我已經得到了最新的phonegap 1.2,並將測試代碼從文檔中放入我的應用程序中。當我在模擬器(xcode 4.2模擬器5.0)和iPad 5上運行我的應用程序時,出現「地理位置錯誤:超時」錯誤。Phonegap定位錯誤:超時

有沒有其他人經歷過這個?這裏是我的代碼:

document.addEventListener("deviceready", onDeviceReady, false); 
     // PhoneGap is ready 
     function onDeviceReady() { 
     var myOptions = { enableHighAccuracy: true }; 
     navigator.geolocation.getCurrentPosition(onSuccess, onError,myOptions);} 

謝謝!

回答

0

我想你錯過了腳本文件導入問題。您需要導入phonegap-1.2.0.js而不是phonegap.js

以下片段在iOS 5模擬器和Device上工作。

<!DOCTYPE html> 
<html> 
    <head> 
     <title>Device Properties Example</title> 
     <script type="text/javascript" charset="utf-8" src="phonegap-1.2.0.js"></script> 
     <script type="text/javascript" charset="utf-8"> 
      // Wait for PhoneGap to load 
      document.addEventListener("deviceready", onDeviceReady, false); 
      // PhoneGap is ready 
      function onDeviceReady() { 
       var myOptions = { enableHighAccuracy: true }; 
       navigator.geolocation.getCurrentPosition(onSuccess, onError,myOptions); 
      } 
      // onSuccess Geolocation 
      function onSuccess(position) { 
       var element = document.getElementById('geolocation'); 
       element.innerHTML = 'Latitude: '   + position.coords.latitude    + '
' + 'Longitude: ' + position.coords.longitude + '
' + 'Altitude: ' + position.coords.altitude + '
' + 'Accuracy: ' + position.coords.accuracy + '
' + 'Altitude Accuracy: ' + position.coords.altitudeAccuracy + '
' + 'Heading: ' + position.coords.heading + '
' + 'Speed: ' + position.coords.speed + '
' + 'Timestamp: ' + new Date(position.timestamp) + '
'; } // onError Callback receives a PositionError object function onError(error) { alert('code: ' + error.code + '\n' + 'message: ' + error.message + '\n'); } </script> </head> <body> <p id="geolocation">Finding geolocation...</p> </body> </html>
+0

如果您使用PhoneGap構建導入phonegap.js實際上很好:構建將最新的穩定版嵌入爲phonegap.js和phonegap-x.y.z.js。 –

0

iMayur的答案顯示,非本地地理位置API正在工作。如果你第一次在iPhone上調用這個腳本,你將會被問到瀏覽器HTML5 API中的「地理定位訪問問題」。

我覺得這裏是PhoneGap的一個更大的問題:Issuetracker #197

[編輯]

有一個新的問題打開:Issuetracker #304

+2

這些鏈接都不起作用。 – skybondsor

0

不知道他們固定它。 現在假設你的代碼是正確的: navigator.geolocation.getCurrentPosition(onSuccess,onError,{'enableHighAccuracy':true,'timeout':10000}); 添加超時部分,它不應該給超時錯誤了。

+0

仍然出現錯誤 – vinesh