Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

navigator.geolocation.getCurrentPosition always getting timeout in Android until GPS/Location is OFF

app.controller('dashboard', function($scope){
    $scope.getPosition = function(position){
        $scope.acc = position.coords;
        $scope.lat = position.coords.latitude;
        $scope.lng = position.coords.longitude;
        $scope.$apply();
    };
    $scope.getPositionErr = function(error){
        alert('code: '    + error.code    + '\n' +
                  'message: ' + error.message + '\n');
    };
    navigator.geolocation.getCurrentPosition($scope.getPosition, $scope.getPositionErr, {maximumAge: 0, timeout: 6000, enableHighAccuracy:false});
});

I am using Angular JS and Cordova for an Android app. This code is working fine on desktop but not on Nexus 4 with Lollipop. Also controller code is executing after deviceready as per requirement from Cordova

I have tried

  • Setting HighAccuracy to false
  • Removing geolocation plugin so that it uses default location from Wifi
  • Device reboot
  • Clearing browser cache
  • Airplane mode on/off
  • Wifi on/off
  • Mobile data on/off

But I am unable to get lat, long and geolocation always gets timeout.

It only works when I enabled Location/GPS from setting. After enabling it code is working as expected.

like image 902
A dev Avatar asked Nov 26 '14 18:11

A dev


2 Answers

navigator.geolocation.getCurrentPosition is only for GPS when option "enableHighAccuracy" is set as 'true'(default). That's why you are not able to get current location via mobile device. About "it is working on desktop", I believe you must have opened GPS setting in your browser.

Usually I used code below t o get position no matter GPS is open or not.

var positionOption = { timeout: 500, enableHighAccuracy: true };
var gpsSunccuss = function(currentPosition) {
    //use gps position
};
var gpsFailed = function() {
    //use some 3rd party position solution(get position by your device ip)
    getPositionBy3rdParty();
};
navigator.geolocation.getCurrentPosition(gpsSunccuss, gpsFailed, positionOption);

On the other hand, you could set enableHighAccuracy as 'false'.

like image 75
Jack He Avatar answered Oct 22 '22 02:10

Jack He


I had identical issue and this fixed it for me

  {enableHighAccuracy:false,maximumAge:Infinity, timeout:60000}
like image 44
rodling Avatar answered Oct 22 '22 01:10

rodling