Geolocationworks first time, then fails


#1

Heey, so i’ve noticed that the geolocation works when the app starts but when i go to another view and go back to the view with the controller that needs it it doesn’t work, i tried to recreate it in a codepen and there it sort of sometimes kinda works and idk what’s happening, is there anyone that knows why it doesn’t get my location when i go back to that controller/view?

CODEPEN:

See the Pen Toggle: 1.0.0-beta.11 by Sjoerd de Wit (@sjerd) on CodePen.

Also found the exact same issue on the ngcordova github https://github.com/driftyco/ng-cordova/issues/379


#2

I recommend you ngCordova, done by ionic guys. So nice!

But, if you can’t or don’t want to use it, you have to choices:

First one, wrap geolocation success callback into a $scope.$apply();

$scope.$apply(function() {
    //whateve here
    //you can use $scope
    $scope.done = true;
});

Even, you can call $scope.$apply() without arguments, but there is some differences.

Another way is to use deferred object (Like ngCordova do)

Hope this helps


#3

i’m actually using ngCordova :slight_smile: just wanted to make a quick pen to demonstrate the issue. and i’ve tried wrapping it with $scope.$apply and $timeout and none of them updated my location…


#4

Is this problem only apparent with ios8 for you? We had issues exactly as you described which were related to the cordova geolocation plugin. If this is the actual problem I’ll find the fix that worked for us and post it for you.


#5

No, it’s accross all platforms browser/android/ios


#6

Did you find a solution or a workaround?


Second time asking help on this ! help me with $cordovageolocation timing out issue( < android 4.4)
#7

I did actually, i’ve put the location update in my main ctrl src:

		var mainloop;
	
	$scope.startloop = function(){
		if ( angular.isDefined(mainloop) ) return;
		mainloop = $interval(function(){
			
			var options = {
			  enableHighAccuracy: true,
			  maximumAge: 0,
			  timeout: 10000
			};
			
			$cordovaGeolocation.getCurrentPosition(options).then(function (pos) {
				latlong =  { 'lat' : pos.coords.latitude, 'long' : pos.coords.longitude };
				$rootScope.currentLocation = latlong;
			}, function(err) {
			});
		},30000);
	};
	
	$scope.stoploop = function(){
		if (angular.isDefined(mainloop)) {
			$interval.cancel(mainloop);
			mainloop = undefined;
		}
	};
	
	$scope.$on('$destroy', function() {
      // Make sure that the interval is destroyed too
      $scope.stoploop();
    });

	var app = document.URL.indexOf( 'http://' ) === -1 && document.URL.indexOf( 'https://' ) === -1;
	if ( app ) {
		
		cordova.plugins.backgroundMode.onactivate = function () {
			$scope.startloop();			
		};
		cordova.plugins.backgroundMode.ondeactivate = function() {
			$scope.startloop();
		};
	}
		
	$scope.startloop();

#8

I’m running into the same issue as you, can you post your solution?


#9

I’m having the exact same issue. Anyone know a fix?


#10

I had solved it, and guilty also of not posting the solution. :confused:

It seems in iOS 8 the GPS permissions had fundamentally changed. I had to disable high accuracy in the options for this the plugin to work. I’m not sure of the root cause, but this was the work around that I’ve used with great success.

The plugin likely needs some updates from someone more familiar with ObjC.

var options = {
  enableHighAccuracy: false,
};

Edit: Somehow I now have two accounts. Go figure…