2012-07-04 45 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;