微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – phonegap geolocation总是在超时时失败

我在我的应用程序中使用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));
         }   
     }
}

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

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

解决方法:

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

SOLUTION:重新启动设备.

就这样.

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐