Timeout about geolocation always reached when position mode is set to “Device only”

不问归期 提交于 2019-12-14 04:16:36

问题


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


回答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).




回答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);

                            //[..]
                        }
                    );
                }
            );



回答3:


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.



来源:https://stackoverflow.com/questions/29411697/timeout-about-geolocation-always-reached-when-position-mode-is-set-to-device-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!