phonegap geolocation always fail on timeout

2019-01-18 03:28发布

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.

11条回答
在下西门庆
2楼-- · 2019-01-18 03:48

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

查看更多
淡お忘
3楼-- · 2019-01-18 03:48

Restart your phone or Go to google map and check if gps is working over there. I got it working.

查看更多
ゆ 、 Hurt°
4楼-- · 2019-01-18 03:49

I restarted and restarted. I reset my phone to factory settings.

But nothing worked.

Until I set enablHighAccuracy from true to false.

And... Tada.... it works.

So :

var options;

options = {
    maximumAge: 30000,
    timeout: 5000,
    enableHighAccuracy: false
};

This used to work fine using PhoneGap 2.6.0. I needed to make this change since I'm now using Phonegap 3.3 - tested on a Wiko Cink+ phone.

查看更多
三岁会撩人
5楼-- · 2019-01-18 03:53

If you are running on an emulator, it may be because the device does not have a geolocation set. Try setting the location by running the following, assuming your android emulator is on port 5554:

telnet localhost 5554
geo fix -0.001 51.5
查看更多
6楼-- · 2019-01-18 03:53

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?

查看更多
Juvenile、少年°
7楼-- · 2019-01-18 03:55

It could sound stupid, but, did you activate the "Use Networks" option?

Go to Settings -> Location and security -> Use networks; and active it. I have passed all the afternoon watching the problem and it was that.

查看更多
登录 后发表回答