I'm getting crashes with the unhandled exception "A geolocation error occured: PositionUnavailable"
But the only place that I think I am calling anything geolocation related is the following:try { await geolocator.GetPositionAsync(timeout: 10000, cancelToken: cancelSource.Token, includeHeading: true) .ContinueWith(t => { // Some code to set the center }); } catch (XLabs.Platform.Services.Geolocation.GeolocationException) { // we just ignore it... }
I guess my questions are, is this going to catch the exception (and if so, why isn't unhandled)
and is there something else that is trying to use the location and thus throwing this exception?
Answers
Hi @ChrisMcBride !
Are you checking if the GPS is enable ? And more, are you instantiate right the object?
Ex.
if (this.geolocator != null)
return;
this.geolocator = DependencyService.Get ();
You could always just catch the Exception class
Ex:
@jbravobr you have a good point, but if geolocator is null then I'll get a null reference exception. And my understanding is, if GSP isn't enable, then the GeolocationException will be thrown. Which I'm trying to handle.
@Cody Sure, I can catch every exception under the wind, but that really isn't what I want to do. I don't want to mask other exceptions.
The point is I am trying to handle a specific exception. And that exception is not being handled. Either my catch is wrong, or someone else is throwing the exception. Is it possible that this exception is thrown when I create the geolocator object?
Hi @ChrisMcBride !
So the point is your are executing the method inside a Task, so to handle the executing code you need to Wait the Task and than catch the Exception (if it's occurs). If already start to thinking that you will loose the asynchronism, think again. As soon your code throw an Exception all the UI will crash (as the App crash too), so if you are expect dealing with some part of your code throw an Exception the right thing to do is .Wait() the Task inside a TRY / CATCH block and deal with the expect Exception.
Look here at the MSDN documentation, I think it's more clarifying
https://msdn.microsoft.com/en-us/library/dd537614(v=vs.110).aspx
But fell free to asking !
The problem is that the exception occurs in another task. To handle that you should do something like that, so that you can handle the exception in the right task.
Alvaro that doesn't make sense. "some code to set the center" isn't throwing the GeoLocationException, "GetPositionAsync" should be throwing the exception. The "continuewith" should allow me to continue even when the GetPositionAsync throws an exception.
@ChrisMcBride
Do you get this behavior is you use the standard Forms GeoCoder?
I use it to do a reverse lookup for a port/city combo, if there is no match it returns a null, it doesn't throw an exception.
The function only takes the 1st result as I didn't have time to handle multiple results.
Reverse lookup is much better in Android.
We are developing a iOS shopping cart application in c# and visual studio 2017 for xamarin, While connecting to map, i got a error like'a geolocation error occurred position unavailable', request you to help us to resolve this issue
Which version of Visual Studio are you using? Are you developing on macOS or Windows? When developing on Windows and using the iPhoneSimulator, I noticed that I received this error. I resolved this by:
I think this has to be done for every time you launch the iPhoneSimulator.