phonegap地理位置在超时时总是失败

gsu*_*oyb 16 android timeout geolocation sencha-touch-2 cordova

我在我的应用程序中使用sencha-touch 2.0和phonegap 2.0.0来检索用户的位置.在我的locahost上运行时,一切正常.但是,当将.apk加载到我的Android 15 api的设备(使用eclipse和adt插件)时,每次调用getCurrentLocation或watchPosition都不会返回...

这是我的代码:

geoOn: function(){
    var geoReady = navigator.geolocation || undefined;

    var onSuccess = function(position){
            Top5.app.alert('Geolocation success '+String(position.coords.latitude) + ' ' + String(position.coords.longitude),'Geolocation');
            var scope = Ext.getCmp('nestedList');
            scope.updateDistance(position.coords);
    };

    var onFailure = function(error){Top5.app.alert('Geolocation failed: '+String(error.code) + ' '+String(error.message),'Geolocation');};
    if (geoReady) {
        this.watchId = navigator.geolocation.watchPosition(onSuccess ,onFailure,{timeout:6000,maximumAge: 3000,enableHighAccuracy: true});    
    }
    else{
        Ext.device.Geolocation.watchPosition({
                 frequency: 3000, // Update every 3 seconds
                 callback: function(position) {
                        this.updateDistance(position.coords);
                 },
                 failure: function() {
                   console.log('Geolocation Failure!');
                 },
                 scope:this
        });
    }
 },
 geoGet: function(){
     var geoReady = navigator.geolocation || undefined;
     if (geoReady) {

         var onSuccess = function(position){
             Top5.app.alert('Geolocation successful!!!');
             var scope = Ext.getCmp('nestedList');
             scope.updateDistance(position.coords);          
         };
         var onFailure = function(error){Top5.app.alert('Geolocation failed: '+String(error.code) + ' '+String(error.message),'Geolocation');};
         navigator.geolocation.getCurrentPosition(onSuccess,onFailure);
     }
     else{}
 },
 geoOff:function(){

     var geoReady = navigator.geolocation || undefined;
     if (geoReady) {
         navigator.geolocation.clearWatch(this.watchId);
         this.watchId = null;
     }
     else{
         Ext.device.Geolocation.clearWatch();
     }

 },
 updateDistance:function(coords){
     Top5.app.alert('updateDist','');
     var scope = Ext.getCmp('nestedList');
     var lat = coords.latitude,lon = coords.longitude;
     var store = scope.getStore();
     var i,record;
     for(i = 0; i < store.data.all.length; i++)
     {
         record = store.data.all[i];
         if(record.data.locationX){
            record.set('distance',Top5.app.getDistance(record.data.locationX,record.data.locationY,lat,lon).toFixed(3));
         }   
     }
}
Run Code Online (Sandbox Code Playgroud)

更新:所以我走出了我的大楼,它工作了......我需要经常出去.
但是,当我禁用gps时,我认为geoLocation将使用wifi连接找到我的位置 - 但它很复杂(我设置enableHighAccuracy:false).这是为什么?

更新: 让我重新解释一下我的问题:navigator.geolocation.watchPosition是否可以同时使用GPS信号和wifi/g3信号?如何仅使用互联网连接检测用户位置?目前,我的代码仅适用于GPS,当禁用该选项或信号被阻止时,地理位置无效.

Art*_*hur 32

我知道也许为时已晚,但今天我在同样的问题上挣扎了!解决方案结果非常简单!

SOLUTION:重新启动设备.

就这样.

问题是,你永远不知道我的用户何时会遇到这种错误,所以如果你的应用程序在很大程度上依赖于地理定位,我建议你在位置选项中设置超时,navigator.geolocation.getCurrentPosition(geoSuccess, geoError, {timeout: 15000})并提醒用户有关问题和可能的解决方案.

PS请记住,Geolocation可以超时,例如,如果移动数据也被关闭,那么重启并不总能解决它.在iPhone上,它使用手机数据来获取您的位置,但在Android上,看起来手机无法访问手机数据,除非3G开启


小智 7

这听起来很愚蠢,但是,你是否激活了"使用网络"选项?

转到设置 - >位置和安全 - >使用网络; 并激活它.我整个下午都在看着这个问题,就是这样.


Ala*_*ink 6

我重新启动并重新启动.我将手机重置为出厂设置.

但没有任何效果.

直到我将enablHighAccuracy从true设置为false.

并且... Tada ......它有效.

所以:

var options;

options = {
    maximumAge: 30000,
    timeout: 5000,
    enableHighAccuracy: false
};
Run Code Online (Sandbox Code Playgroud)

这曾经使用PhoneGap 2.6.0正常工作.我需要做出这个改变,因为我现在正在使用Phonegap 3.3 - 在Wiko Cink +手机上测试过.


gsu*_*oyb -4

我已经设法解决了......但是,我不知道实际上是什么解决了它。我所做的只是改变我的模型的结构之一。有人能看出与地理定位的联系吗?