GetGeopositionAsync() never finishes, works pefect the second time it is called

0

I have used GetGeoPositionAsync() in my app and it works fine in the code of another feature, however when using it in the feature I am currently implementing, it freezes the first time I try to call it, and works the second time. I call this function when navigating from another page. Here's the code:

   private async Task GetLocation()
   {
        Geolocator myGeolocator = new Geolocator();
        Geoposition myGeoposition = null;
        try
        {
            myGeoposition = await myGeolocator.GetGeopositionAsync();
        }
        catch (Exception ex)
        {
            if ((uint)ex.HResult == 0x80004004)
            {
                MessageBox.Show("Unauthorized access.");
            }
        }
    }

I tried browsing SO, but haven't found a working solution for this. Why does it work in some pieces of code, and in some it just doesn't. What is the cause of this behaviour?

c#
silverlight
windows-phone-8
windows-phone
asked on Stack Overflow Sep 10, 2013 by Kajzer

2 Answers

4

Ok, so I found a solution:

        try
        {
            IAsyncOperation<Geoposition> locationTask = null;
            try
            {
                locationTask = myGeolocator.GetGeopositionAsync(TimeSpan.FromMinutes(1), TimeSpan.FromSeconds(3));
                myGeoposition = await locationTask;
            }
            finally
            {
                if (locationTask != null)
                {
                    if (locationTask.Status == AsyncStatus.Started)
                        locationTask.Cancel();

                    locationTask.Close();
                }
            }
        }
answered on Stack Overflow Sep 12, 2013 by Kajzer
0

This sounds like the position is cached! The first time you call the method it tries to get your position (this can take many seconds if you are inside a building), the second time it seems to use the cached position.

answered on Stack Overflow Sep 10, 2013 by Malte Goetz

User contributions licensed under CC BY-SA 3.0