phonegap应用程序不起作用

时间:2012-07-04 09:43:49

标签: cordova geolocation

我是phonegap的新手。我正在创建一个获取设备位置的应用程序...我已经测试了三星galaxy tab和htc,但没有做任何事情......我试着获得设备的速度,并将其显示在一个屏幕中的div。这是我的代码:

地理位置:

// 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的扩展程序Ripple中进行了测试,它工作正常,更改了速度值,并且工作正常......但是对于设备,我不知道为什么不这样做。 为什么会这样? 此致,丹尼尔

我已经读过可能需要在var选项中添加{enableHighAccuracy:true} ...但我不明白这一点......

1 个答案:

答案 0 :(得分:3)

Daniel先生,首先我建议您尝试使用这个常见的PhoneGap应用来测试设备是否可以使用,如果是,它会提示您显示警告消息,同时,如果它正在工作完美,所以你的代码有问题,所以回去检查一下。你在这里:

// 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;
相关问题