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

Deadly 提交于 2019-12-04 15:12:30

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

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

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

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.

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