Ionic native geolocation

I’m having difficulties with the Geolocation.

This is the code I’m using to get the location:

platform.ready().then(() => {
			//
			var options = {
						  enableHighAccuracy: false,
						  timeout: 5000,
						  maximumAge: 0
						 };
						 
			this.geolocation.getCurrentPosition(options).then((position) => {
				  this.showschedule(position.coords.latitude, position.coords.longitude);
				  this.loader.dismiss();
				}, (err) => {
					console.log("Error getting location: "+JSON.stringify(err));
				  this.showschedule(0,0);
				  this.loader.dismiss();
				});
	 });

My problem is that in some cases without any reasons I will get an error like:
Error getting location: {}

Other apps are able to access location, restarting the phone does solve the problem or sometimes the problem disappears on it’s own.

When ionic has difficulties getting the location other apps have no problem getting it, so it’s not that the whole Location of the phone have crashed.

This happened on multiple Android devices.

I saw a few forums/threads about this but no real solution anywhere, for a location sensitive app this is kind of a big problem for me.
Any ideas or suggested workarounds on how can I resolve this?

EDIT

Doing some more testing the behaviour since to occur on Nougat devices, it works fine on older versions.

Thank you,

Since I was not able to figure it out why it’s sporadically working or not I came up with a temporary workaround till there is a better solution:

I have downloaded and installed this plugin: https://github.com/snokleby/cordova-fusedlocation which uses fusedlocation provider, I did add some small changes so it reports the timestamp of the location and the accuracy, but at least I can get a location every time now.

Not ideal because location watcher still doesn’t or sometimes doesn’t work, but better than randomly not getting locations at all.

Opened to better suggestions.

Load variables into data
example:
var opciones = {maximumAge:0, timeout: 4000, enableHighAccuracy: false}
this.geolocation.getCurrentPosition(opciones).then((resp) => {
this.lat = resp.coords.latitude;
this.long = resp.coords.longitude;
this.alt = resp.coords.altitude;
//console.log(resp);
}).catch((error) => {
console.log(‘Error getting location’, error);

	});

Then call the function by passing those variables

Hi @GustavoSanDiego, no disrespect but how is that different to my original question?

I have had problems passing variables to functions, for that reason sometimes I prefer to load them into global viarables

The way I pass it I have it in an app that I’m working on at this moment

I don’t know what the issue might be in an explicit fetch other than imagining that its possible that at the time you request it, depending on the state of the gps hardware/ resource allocations you get errors (total guess here). Have you tried using the subscription model instead of an explicit fetch? I use this format - haven’t had an issue yet

this.gpsSub = this.geo.watchPosition()
          .subscribe(data => {
            gpsCallback (data);

          })

The subscription model reports data when its available and you can update a class variable with the latest status returned.

I did try adding a watchPosition as well but same experience sometimes it works sometimes it doesn’t. Even when it doesn’t work, all other apps which use position seems to report it correctly.

I did a test, I have removed the Geolocation plugin totally from the project and I have used the native window.navigator.getCurrentPosition instead, which is the HTML5 standard of getting location.

Same experience some times it did work sometimes not, so I think the problem may be caused by the underlying Webkit rather than Plugin/Cordova/Ionic

FusedLocation looks to work reliable, I might make a position watcher for it as well if I need to track the location.

1 Like

I have the same problem on android

I have the same problem on android

[app-scripts] [17:23:49] console.log: Error getting location [object Object]

please help me