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

phonegap geolocation always fail on timeout(phonegap 地理位置总是在超时时失败)

本文介绍了phonegap 地理位置总是在超时时失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using sencha-touch 2.0 and phonegap 2.0.0 in my app to retrieve user's location. When runing on my locahost, everything works just fine. However, when loading the .apk to my android 15 api's device (using eclipse and the adt plugin), every call to getCurrentLocation or watchPosition never returns...

这是我的代码:

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).这是为什么呢?

UPDATE: So I walked out of my building and it worked... I need to go outside more often.
However, when I'm disabling the gps, I thought geoLocation will find my location using wifi connection - but it failes (I'm setting enableHighAccuracy: false). Why is that?

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

UPDATE: Let me rephrase my question: Does navigator.geolocation.watchPosition suppose to work both with GPS signal and wifi/g3 signals? How can I detect user location using internet connection only? currently, my code is working only with GPS, and when that option disabled or signal is blocked, geolocation isn't working.

推荐答案

我已经设法解决了.... 但是,我不知道实际解决了什么问题.我所做的只是改变我模型的一个结构.任何人都可以看到与地理位置的联系吗?

I've managed to work it out.... However , I have no idea what actually solved it. All I've done is to change one of my model's structure. Can anyone see the connection to geolocation?

这篇关于phonegap 地理位置总是在超时时失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:phonegap 地理位置总是在超时时失败

基础教程推荐