2012-07-04 130 views
0

我是手機新手。我正在創建一個獲取設備位置的應用程序...我已經使用galaxy galaxy tab和htc進行了測試,但是沒有做任何事情...我嘗試獲取設備的速度,並將其顯示在在屏幕上的div。這是我的代碼:phonegap應用程序不起作用

地理位置:

// Phonegap loads 
    document.addEventListener("deviceready", onDeviceReady, false); 
    var watchIDgeolocation = null;  
    var watchIDaccelerometer = null;  

// PhoneGap has loaded 
    function onDeviceReady() { 
     var options = { frequency: 1000 }; // I want obtain data each 1 s 
     watchIDgeolocation = navigator.geolocation.watchPosition(onSuccessGeolocation, onErrorGeolocation, options); 
     startWatch(); 
    } 

// Success 
    var onSuccessGeolocation = function(position) { 
     // These are functions that displays data in screen 
     cambioMarchas(position.coords.speed);  
     excesoVelocidad(position.coords.speed);  
     paradaProlongada(position.coords.speed);  
     aceleracionBrusca(position.coords.speed); 
     deceleracionBrusca(position.coords.speed); 
     accidenteDeTrafico(position.coords.speed); 


// ERROR 
    function onErrorGeolocation(error) { 
     alert('code: ' + error.code + '\n' + 
       'message: ' + error.message + '\n'); 
    } 

我在紋波,Chrome瀏覽器的擴展測試,並能正常工作,改變速度的值,並能正常工作......但用設備,我不知道爲什麼不。 爲什麼可以? 的問候,丹尼爾

我已閱讀,也許是需要添加{enableHighAccuracy:真正}在VAR選項...但我不明白這一點...

+1

它在模擬器中工作嗎? – Aamirkhan

+0

是的,在鉻的波紋擴展 – Daniel

回答

3

好丹尼爾,首先我建議你試試這個普通的PhoneGap應用程序來測試設備是否準備好使用或者不使用,如果是的話,它會通過顯示警報消息提醒你,同時,如果它工作的很好,那麼你的代碼出現問題,所以回去n覈實。您在這裏:

// Global variable that will tell us whether PhoneGap is ready 
    var isPhoneGapReady = false; 
    function init() { 
// Add an event listener for deviceready 
    document.addEventListener("deviceready", 
    onDeviceReady, false); 
// Older versions of Blackberry < 5.0 don't support 
// PhoneGap's custom events, so instead we need to perform 
// an interval check every 500 milliseconds to see whether 
// PhoneGap is ready. Once done, the interval will be 
// cleared and normal processing can begin. 
    intervalID = window.setInterval(function() { 
    if (PhoneGap.available) { 
    onDeviceReady(); 
      } 
     }, 500); 
    } 

    function onDeviceReady() { 
    window.clearInterval(intervalID); 
// set to true 
    isPhoneGapReady = true; 

    alert('The device is now ready'); 
    } 
// Set an onload handler to call the init function 
    window.onload = init; 
相關問題