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

4

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

Selfsuggestion answered 26/11, 2014 at 18:6 Comment(7)
six seconds may not be long enough for the time out. Most largescale apps run at least 30 seconds for location timeouts. Have you tried a longer timeout?Cestus
Yes I have tried with 1 minute and without time as well.Selfsuggestion
Now this is something similar. Open Geo Location Exmaple in Chrome browser in Android. Simple geolocation example from w3schools. You won't be able to get your lat, long until unless you turn on your Location/GPS.Selfsuggestion
Are you using any location spoofing applications like "fake GPS"? then remove it and try again.Yuille
I am just trying desktop Chrome with default settings. I am using chrome as end user.Selfsuggestion
Recent observations, with enableHighAccuracy:false Nexus 4, OS 5.0 => GPS required to get position Moto G, OS 4.4 => GPS required to get position Now miracle Samsung I9300 Galaxy S III, OS 4.3 => GPS NOT required to get position, from same code. This question is still unanswered, lets see if this can help someone in finding solution.Selfsuggestion
Stop flagging about a bounty that was automatically awarded years ago! In all this time you could have read up about bounties and you'd have the answer. We cannot do anything about it.Subdivide
W
10

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

Walsh answered 2/12, 2014 at 2:32 Comment(6)
GPS is not needed for geolocation to work. It can be run in desktop browser without opening any GPS settings.Selfsuggestion
@NatGeo Could you please have a try on mobile device?Walsh
I am already trying on mobile device with sample code above and its working on desktop chrome and not on phonegap application. And all these I have already mentioned in question.Selfsuggestion
How bounty can be awarded if I didn't received correct answer. I want to know where in any official documentation it is written that "getCurrentPosition is only for GPS" for mobile devices. If this is just observation, then I have already observed this before posting my question, and this is what my question was about, that why mobile devices are not getting position if GPS is off as compared to desktop browsers.Selfsuggestion
@NatGeo I apologize. I forgot to let you know an important option: enableHighAccuracy . When it has been set as true, only GPS is enabled; when it has been set as false, IP is also okay for users to get position. I have updated my answer.Walsh
In my question I have already said, that I have tried both true/false for enableHighAccuracy. Thank you for help.Selfsuggestion
N
5

I had identical issue and this fixed it for me

  {enableHighAccuracy:false,maximumAge:Infinity, timeout:60000}
Natalianatalie answered 9/8, 2015 at 18:38 Comment(2)
Changing the maximumAge to Infinity solved the problem for me. Thank you!Electropositive
I concur, for my usecase having options as {enableHighAccuracy:false, maximumAge:Infinity} solved the problem.Behold
M
3

I think setting maximumAge to Infinity only works because in that case it just gives you back the previous location when the timeout occurs. I wouldn't consider that working.

Metamorphose answered 5/5, 2022 at 5:29 Comment(0)
C
-3

you can try this.

{enableHighAccuracy:true, Infinity:Infinity, timeout:2000}
Chantey answered 17/5, 2019 at 12:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.