locked
GetGeopositionAsync not working as expected RRS feed

  • Question

  • I need to determine the user's location at initial startup, and subsequently only when a settings command is explicitly selected.

    I use the following code:

            public static async Task<Point> GetUserLocation()
            {
                try
                {
                    Geoposition pos = await new Geolocator().GetGeopositionAsync(TimeSpan.FromMinutes(60), TimeSpan.FromSeconds(10));
                    return new Point(pos.Coordinate.Longitude, pos.Coordinate.Latitude);
                }
                catch (Exception)
                {
                    // the user doesn't allow location services
                    return new Point(0, 0);
                }
            }

    The typical scenario where the user wants to invoke this code is that they have travelled to a new location, settled in for awhile, then start up my app to show info for their new location.

    This code seems to work well at startup. But, when I travel with my Surface RT, I find that GetGetpositionAsync always returns my home location rather than my new location. I've started Maps and it homes in on a new location (not necessarily where I am, but different than my home location).

    (As I write the sentence above, I realize that on my last trip, I should have uninstalled the app, then re-installed to see if the startup instance of this call worked differently. Next trip!)

    I don't understand exactly how the "maximumAge" parameter is supposed to work, but when I travel-test, it is always the case that it is way more than 60 minutes since I left the home location.

    Any idea what I'm doing wrong?

    Bill


    Tuesday, January 22, 2013 10:05 PM

Answers

  • I believe it is now working correctly. The problem I'm seeing is that the call to GetGeopositionAsync throws a

    TaskCanceledException intermittently. The exception is thrown well before the "timeout" parameter I provided. I added a retry after a 2-second delay, and it now works about 95% of the time. It's a little ugly, but is serviceable for my application.

    Wednesday, January 23, 2013 2:20 AM

All replies

  • Your  Surface RT has no GPS, so your Location is only detected by WLAN-Connections and so on.
    Tuesday, January 22, 2013 10:42 PM
  • I believe it is now working correctly. The problem I'm seeing is that the call to GetGeopositionAsync throws a

    TaskCanceledException intermittently. The exception is thrown well before the "timeout" parameter I provided. I added a retry after a 2-second delay, and it now works about 95% of the time. It's a little ugly, but is serviceable for my application.

    Wednesday, January 23, 2013 2:20 AM
  • I don't think this is dependent on HOW the Surface determines its location. It finds SOMETHING even if it's total nonsense. As long as I can get that through GetGeopositionAsync, I'm happy.

    Wednesday, January 23, 2013 2:22 AM