Forum General

Geolocation GetLocationAsync(request) not awaiting

sisaackssisaacks Member ✭✭✭

Can anyone explain to me why this line doe snot actually await?
Location location = await LocationUtility.GetCurrentLocation();

My UI thread does not even display a busy spinner....a few seconds later the UI thread updates.

internal async Task CheckLocationPermissionAndGetGeoLocation(bool isFromPopUp = false)
{
IsBusy = true;

        Plugin.Permissions.Abstractions.PermissionStatus permissionStatus = await PermissionCheck(Permission.Location);

        if (permissionStatus == Plugin.Permissions.Abstractions.PermissionStatus.Granted)
        {
            Location location = await LocationUtility.GetCurrentLocation();

            if(location != null)
            {
                await SaveGeolocation(location, isFromPopUp);
                await SolveAndSend();
            }
            else
            {
                PopUpUtility.ShowPopUp("Warning", "Latitude and Altitude could not be retrieved.");
            }
        }

        IsBusy = false;
    }

public static async Task GetCurrentLocation()
{
Location location = null;
string title = "Geo Location Services";

        try
        {
            GeolocationRequest request = new GeolocationRequest(GeolocationAccuracy.Best, TimeSpan.FromSeconds(5));
            location = await Geolocation.GetLocationAsync(request);


            // if altitude did not come back the first time...lets try again
            if(location != null && !location.Altitude.HasValue)
            {
                request = new GeolocationRequest(GeolocationAccuracy.Best, TimeSpan.FromSeconds(5));
                location = await Geolocation.GetLocationAsync(request);
            }
        }
        catch (FeatureNotSupportedException fnsEx)
        {
            App.logger.Error($"Location Untility GetCurrentLocation: {fnsEx.Message.ToString()}");
            await Application.Current.MainPage.DisplayAlert(title, "Feature not suppported.", "Ok");
        }
        catch (FeatureNotEnabledException fneEx)
        {
            App.logger.Error($"Location Untility GetCurrentLocation: {fneEx.Message.ToString()}");
            await Application.Current.MainPage.DisplayAlert(title, "Feature not enabled.", "Ok");
        }
        catch (PermissionException pEx)
        {
            App.logger.Error($"Location Untility GetCurrentLocation: {pEx.Message.ToString()}");
            await Application.Current.MainPage.DisplayAlert(title, "Feature permission not granted.", "Ok");
        }
        catch (Exception ex)
        {
            App.logger.Error($"Location Untility GetCurrentLocation: {ex.Message.ToString()}");
            await Application.Current.MainPage.DisplayAlert(title, "Feature not available.", "Ok");
        }

        return location;

    }
Sign In or Register to comment.