fzl
fzl

Reputation: 171

GeoCoordinateWatcher StatusChanged event in WP8

I am developing an application that tracks my route. When the user starts the application, I want to make sure whether the GPS is enabled or not. So I wrote this:

protected override void OnNavigatedTo(NavigationEventArgs e)
        {            
            base.OnNavigatedTo(e);
            Transporter._watcher.StatusChanged += _watcher_StatusChanged;
        }  
void _watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
        {          
            button1.IsEnabled = false; //appbar buttons
            button2.IsEnabled = false;

        switch (e.Status)
        {

            case GeoPositionStatus.Initializing:
                button1.IsEnabled = false;
                button2.IsEnabled = false;
                menuitem1.Text = "searching gps signal...";                    
                break;

            case GeoPositionStatus.NoData:
                button1.IsEnabled = false;
                button2.IsEnabled = false;
                menuitem1.Text = "no location info";                    
                break;

            case GeoPositionStatus.Ready:
                button1.IsEnabled = true;                    
                menuitem1.Text = "gps ok";                    
                break;
        }
    }       

And my problem is, when the user forget to turn on the GPS before starting the application, they have to minimize the app, turn the gps on, and go back to the app. I tried it out, OnNavigatedTo event fires, so as StatusChanged, but nothing happens. Why is that happening? It should get to the Initializing Status and then Ready.

Upvotes: 0

Views: 231

Answers (1)

Romasz
Romasz

Reputation: 29792

If you want to check for the GPS running, you could use Geolocator.LocationStatus property. It's values will tell you if it's initializing, switched off and so on.

So maybe this will help:

protected override void OnNavigatedTo(NavigationEventArgs e)
    {            
        base.OnNavigatedTo(e);
        if (yourGeolocator.LocationStatus == PositionStatus.Disabled)
        {
            // do your stuff for disabled GPS
        }
        else  NotDisabled();
    } 

Upvotes: 2

Related Questions