Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
173 views
in Technique[技术] by (71.8m points)

android - phonegap geolocation always fail on timeout

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...

here is my code:

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));
         }   
     }
}

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?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I know that maybe it is too late, but today i struggled with the same issue! The solution turned out to be very simple!

SOLUTION: Reboot the device.

That's all.

The problem is, that you never know when i user will get this kind of bug, so if your application relies heavily on geolocation i recommend you set a timeout in location options navigator.geolocation.getCurrentPosition(geoSuccess, geoError, {timeout: 15000}) and alert user about the problem and possible solution.

P.S. Keep in mind, that Geolocation can timeout for example if mobile data is turned off too, so reboot won't always fix it. On iPhone it uses cellular data to get your position, but on Android, it looks like the phone does not have access to cellular data unless 3G is turned on


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...