Windows phone 8.1 GeoLocator GeoPositionAsync not working unless in code behind

0

I spent several hours researching and trying various things to solve apparently a common issue in the 8.1 SDK where the GeoLocator.GetGeoPositionAsync call never returns or falls threw to an exception it often throws (per Google search) ex.HResult == 0x80004004. After doing some digging, I noticed that this error was silently hidden inside the DesiredAccuracyInMeters if not set.

Even after this, the only way I was able to get this to work is by placing the code inside the code behind of the windows phone view I was trying to have call this method inside a button click. I am starting to think that there is some threading issue that I don't fully understand? I'm not great at UI threads and all that, but I think I am using the proper usage of async and await for it to follow it all the way back.

Here is the code. The GeoLocator.GetGeoPositionAsync method is inside a phone extension library I created nd the class looks like this:

 public class PlaceFinder : IPlaceFinderProvider<MapRoute>
{
    private Dictionary<string, bool> settings = UserSettingsProvider.GetSetting<bool>(new List<string>() { "HasUserConsent" });

    public async Task<GeoLocationModel> GetUserLocationAsync()
    {
        var geoLocation = new GeoLocationModel();

        try
        {

            if (settings.Any(x => x.Key == "HasUserConsent" && x.Value == true))
            {
                geoLocation.HasUserConsent = true;
                var locator = new Geolocator
                {
                    DesiredAccuracyInMeters = 500,
                    DesiredAccuracy = PositionAccuracy.High,
                    MovementThreshold = 1
                };


                var position = await locator.GetGeopositionAsync(TimeSpan.FromMinutes(5), TimeSpan.FromSeconds(10));

                geoLocation.UsersLongitude = position.Coordinate.Longitude;
                geoLocation.UsersLatitude = position.Coordinate.Latitude;

            }
            else
            {
                throw new LocationException(LocationException.LOCATION_SERVICES_USER_ACCEPTANCE_FAILED);
            }
        }
        catch (LocationException)
        {
            throw;
        }
        catch (UnauthorizedAccessException ex)
        {
            throw new LocationException(LocationException.LOCATION_SERVICES_UNAVAILABLE);
        }
        catch (Exception ex)
        {
            if ((uint)ex.HResult == 0x80004004)
            {
                throw new LocationException(LocationException.LOCATION_SERVICES_TURNED_OFF);
            }
            throw ex;
        }

        return geoLocation;
    }

Then the call to use the above in a separate helper method:

 public class GeoLocationHelper
{

    public async static Task<GeoLocationModel> FindUserLocation()
    {
        PlaceFinder finder = new PlaceFinder();
        return await finder.GetUserLocationAsync();
    }

Inside the same code behind file,

This does not work:

 location = await GeoLocationHelper.FindLocation();

Using the code directly from my custom library works:

 PlaceFinder finder = new PlaceFinder();
 var userLocation = await finder.GetUserLocationAsync();

So my question is, why would the direct call to the library work and the extra helper class cause it to never return?

c#
windows-phone-8
geolocation
maps
async-await
asked on Stack Overflow Dec 16, 2014 by gcoleman0828 • edited Dec 16, 2014 by Jude

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0