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?
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();
}
}
}
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.
User contributions licensed under CC BY-SA 3.0