2

I own a Ionic application (Cordova).

I have this JS code aiming to get the position of the Android's device:

$cordovaGeolocation.getCurrentPosition({
                    enableHighAccuracy: true,
                    timeout: 15000
                }) 

$cordovaGeolocation comes from ng-cordova lib.
I successfully checked that the plugin org.apache.cordova.geolocation is updated with the last current version.

Some users complain about more 15 seconds for the first query!
Note that the timeout is set to 15000 ms => 15 seconds.
Meaning that the device did not succeed to establish the position.

After investigating, I figured out the issue:
those users had set their position mode to "Device Only" or also called "GPS only" in their device's setting.

When the user switches to "High Accuracy mode", the whole works in less than a second.

How to fix this issue without advising user to switch to "High accuracy mode" ? Is it a bug?

Note that I also tested with enableHighAccuracy: false, but same result.

I'm not the only one having this "big" issue:
Phonegap - Geolocation with PowerSaving and GPS Only Mode
but unanswered..

4

3 回答 3

2

I have seen you issue on github. I'm using ng-cordova plugin. I use the below workaround to get GPS-only working. As low-accuracy is faster, the timeout is 4s.

$cordovaGeolocation.getCurrentPosition({enableHighAccuracy: false, maximumAge: MAXAGE, timeout: 4000})
            .then(
                function (position) { //success Low-Accuracy
                    console.log('getCurrentPosition: HighAccuracy false: Ok!');
                    //[..]
                },
                function(err) { //error Low-Accuracy
                    console.log(err);
                    $cordovaGeolocation.getCurrentPosition({enableHighAccuracy: true, maximumAge: MAXAGE, timeout: 10000})
                    .then(
                        function (position) { //success High-Accuracy
                            console.log('getCurrentPosition: HighAccuracy true: Ok!');
                            //[..]
                        },
                        function(err) { //error High-Accuracy
                            console.log('getLocation: ERRO: ' + ERROR[err.code] + ' => ' + err.message);

                            //[..]
                        }
                    );
                }
            );
于 2015-07-13T19:14:35.803 回答
1

I struggle that issue for three days, and there is no solution. It just not working and never been. So I will try use another tool, because cordova just don't fit for geolocation purposes. Now I'm focus on java and I want do this properly (ios was a bonus, when I choose cordova).

于 2015-05-08T07:47:21.033 回答
1

I've been struggling with the same issue for the whole day and i've found out that cordova geolocation plugin for android uses geolocation capability of the WebView, don't know why, but for WebView 'gps only' is not enough.

So, i've started to search for some alternatives and found https://github.com/louisbl/cordova-locationservices

Plugin is quite awesome but requires android play services. I've written small angular service for geolocation:

 multiplatformGeolocation.inject = ['$q'];
    function multiplatformGeolocation ($q) {
        var self = this;

        this.getCurrentPosition = function (opts) {
            var q = $q.defer();

            self.locationModule.getCurrentPosition(function (result) {
                q.resolve(result);
            }, function (err) {
                q.reject(err);
            }, opts || self.positionOptions);

            return q.promise;
        };

        this.init = function () {
            var PRIORITY_BALANCED_POWER_ACCURACY = 102;
            self.positionOptions = {
                timeout: 20000,
                enableHighAccuracy: false,
                priority: PRIORITY_BALANCED_POWER_ACCURACY
            };

            self.locationModule = window.cordova && window.cordova.platformId == 'android' ? LocationServices : navigator.geolocation;
        }
    }

Note that I've used PRIORITY_BALANCED_POWER_ACCURACY, it lets the device use the network to find current position if gps is disabled.

于 2015-07-08T13:07:09.827 回答