How to catch A geolocation error occured: PositionUnavailable?

ChrisMcBrideChrisMcBride ✭✭USMember ✭✭

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?

Tagged:

Answers

  • jbravobrjbravobr ✭✭ BRMember ✭✭

    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 ();

  • CodyCody ✭✭ USMember ✭✭
    edited July 2015

    You could always just catch the Exception class

    Ex:

    Try
    {
        //something that fails
    }
    catch(Exception ex)
    {
        //will catch any general exception and set message in a string
        string msg = ex.Message;
    }
    
  • ChrisMcBrideChrisMcBride ✭✭ USMember ✭✭

    @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?

  • jbravobrjbravobr ✭✭ BRMember ✭✭

    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 !

  • AlvaroPereiraAlvaroPereira BRMember

    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.

              await geolocator.GetPositionAsync(timeout: 10000, cancelToken: cancelSource.Token, includeHeading: true)
                    .ContinueWith(t =>
                    {
                    try
                {
                     // Some code to set the center
                }       
                    catch (XLabs.Platform.Services.Geolocation.GeolocationException)
                    {
                            // we just ignore it...
                    }                    });
            }
    
  • ChrisMcBrideChrisMcBride ✭✭ USMember ✭✭

    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.

  • NMackayNMackay mod GBInsider, University mod

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

        public class GeoService: IGeoService
        {
            public async Task<GeoPosition> FindAddress(string addressSearch)
            {
                try
                {
                    var geo = new Geocoder();
                    var positions = (await geo.GetPositionsForAddressAsync(addressSearch)).ToList();
    
                    if (positions != null && positions.Any())
                    {
                        return new GeoPosition { Latitude = positions.First().Latitude, Longitude = positions.First().Longitude, Address = addressSearch };
                    }
                    return null;
                }
                catch (Exception ex)
                {
                    var exService = ServiceLocator.Current.GetInstance<IExceptionService>();
                    exService.Error(GetType().Name, ex);
                    return null;
    

    The function only takes the 1st result as I didn't have time to handle multiple results.

    Reverse lookup is much better in Android.

  • ThiyaguPeriyasamyThiyaguPeriyasamy ✭✭ USMember ✭✭

    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

  • Hayden_HancockHayden_Hancock ✭✭ USMember ✭✭

    @ThiyaguPeriyasamy said:
    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:

    1. Launch iPhoneSimulator.
    2. Click Settings (the gear icon).
    3. Under Location, ensure the Choose a location dropdown menu is filled out (e.g. "Apple").

    I think this has to be done for every time you launch the iPhoneSimulator.

Sign In or Register to comment.