In my Windows Phone 8 app, I am trying to use GetGeopositionAsync on the main page to display some items based on user location.
Calling GetGeopositionAsync does not return within the specified timeout, it doesn't return at all.
The code I am using is simple:
Geolocator geolocator = new Geolocator();
geolocator.DesiredAccuracyInMeters = 50;
Geoposition geoposition = null;
try
{
geoposition = await geolocator.GetGeopositionAsync(
maximumAge: TimeSpan.FromMinutes(5),
timeout: TimeSpan.FromSeconds(10));
}
catch (UnauthorizedAccessException ex)
{
// location services disabled or other error
// user should setup his location
}
The solution I was able to find was to create an async wrapper for GeoCoordinateWatcher which seems to be working fine. But I am not too confident in my solution, I would prefer to use GetGeopositionAsync which looks like the recommended way of getting device position in WP8.
UPDATE: other people are reporting same behavior: http://social.msdn.microsoft.com/forums/en-us/wpdevelop/thread/ff166fac-b423-4428-abd8-610bf0102fc0
When are you calling the method to request the geoposition? I found I encountered the same issue when I made the call part of the constructor in my ViewModel.
I was able to fix the problem in my code by adding an OnLoadedCommand and calling the method from there. I've had no further issues since.
This is strange but GetGeoPositionAsync only returns the current position when the Geolocator is initialized with either MovementThreshold and/or ReportInterval.
Geolocator geolocator = new Geolocator();
geolocator.DesiredAccuracyInMeters = 50;
geolocator.MovementThreshold = 5;
geolocator.ReportInterval = 500;
Geoposition geoposition = null;
try
{
geoposition = await geolocator.GetGeopositionAsync(
maximumAge: TimeSpan.FromMinutes(5),
timeout: TimeSpan.FromSeconds(10));
}
catch (UnauthorizedAccessException ex)
{
// location services disabled or other error
// user should setup his location
}
I had this issue when testing on a Device. I had to disable the WiFi on the device in order to get it to work. I know that some people have had the opposite problem working on the emulator. I did not have to do any wrapping. Hope it Helps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With