Vladimir
Vladimir

Reputation: 31

Strange behaviour of Geolocator.GetGeopositionAsync() after first launch

I'm writing Windows Phone 8 app that needs to get location of device (do not track changes, just get location). I added next code to the method OnNavigatedTo() of my start page but after launching app, the progress indicator does not hide even after 10 seconds timeout. But if I navigate to another page and then go back, everything works fine. This happens on the emulator, I don't have a real device. What am I doing wrong?

protected async override void OnNavigatedTo(NavigationEventArgs e)
{
    if(_geoPosition == null)
    {
        try
        {
            var geolocator = new Geolocator();
            geolocator.DesiredAccuracyInMeters = 50;

            _progressIndicator = new ProgressIndicator
            {
                IsIndeterminate = true,
                Text = "Getting current location, please wait...",
                IsVisible = true
            };
            SystemTray.SetIsVisible(this, true);
            SystemTray.SetProgressIndicator(this, _progressIndicator);

            _geoPosition = await geolocator.GetGeopositionAsync(TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(10));

            _progressIndicator.IsVisible = false;
            SystemTray.SetIsVisible(this, false);
        }
        catch (UnauthorizedAccessException)
        {
            MessageBox.Show("Location is disabled in phone settings");
        }
    }
}

Thanks!

UPD: just tried to add this code to empty project and it works fine. Tried to comment out some parts of OnNavigatedTo that I did not include to the snippet and found out that the reason somewhere in initialization of data source for this page. I'm sorry for false alarm.

Upvotes: 3

Views: 1620

Answers (1)

Iris Classon
Iris Classon

Reputation: 5842

Your code works fine for me, try the classic restart VS and the projecy!

The code should work, tested it with an emulator and a device (nokia 820).

Best of luck

Upvotes: 1

Related Questions