Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GetGeopositionAsync does not return

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

like image 722
WriteEatSleepRepeat Avatar asked Feb 26 '13 03:02

WriteEatSleepRepeat


3 Answers

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.

like image 74
Saintchubs Avatar answered Nov 02 '22 03:11

Saintchubs


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
}
like image 26
prathiraj Avatar answered Nov 02 '22 02:11

prathiraj


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

like image 2
kbo4sho88 Avatar answered Nov 02 '22 03:11

kbo4sho88