开发者

How to trap errors during GPS lookup on WP7

开发者 https://www.devze.com 2023-03-31 17:29 出处:网络
I\'m trying to alert the user when GPS is not available (basically when they have geo loc services off)

I'm trying to alert the user when GPS is not available (basically when they have geo loc services off)

The following is my lookup case statement and I've noticed that in the emulator I hit the "" status so I couldn't return an error here as it was still working as expected

    void MyStatusChanged(GeoPositionStatusChangedEventArgs e)
    {
        switch (e.Status)
        {
            case GeoPositionStatus.Disabled:
                client.ClientCallBackWithError("fail");
                break;
            case GeoPositionStatus.Initializing:
                var y = "initializing location service,";
                break;
            case GeoPositionStatu开发者_StackOverflow中文版s.NoData:
                // The location service is working, but it cannot get location data
                // Alert the user and enable the Stop Location button
                var z = "data unavailable,";
                break;
            case GeoPositionStatus.Ready:
                var zzz = "receiving data, ";
                break;

        }
    }

So inside my calling page/view I decided maybe I could wait for 10 seconds to see if this is ever hit or not .. and if not I could just throw up an error / etc

warning huge hack as I'm losing hope

    private void FindByGps_Click(object sender, RoutedEventArgs e)
    {
        gpsStarted = false;
        gpsEnded = false;

        progressHelper.StartProgressStuff(this.progress);
        gpsStarted = true;
        gpsLocationLookupService.StartLocationService();
        this.Dispatcher.BeginInvoke(() => ListenForCallbackDuringGpsLookup(0));
    }

    private object ListenForCallbackDuringGpsLookup(int counter)
    {
        if (gpsStarted && !gpsEnded && counter < 12)
        {
            //keep looking until the timer runs out ...
            counter = counter + 1;
            this.Dispatcher.BeginInvoke(() => SleepForASec());
            ListenForCallbackDuringGpsLookup(counter);
        }
        else if (gpsStarted && gpsEnded)
        {
            gpsStarted = false;
            gpsEnded = false;
        }else{
            this.Dispatcher.BeginInvoke(() => SetCurrentLocationAndLaunchFindKiosks(null, "Failed to locate you by GPS"));
        }

        return null;
    }

    private object SleepForASec()
    {
        Thread.Sleep(1000);

        return null;
    }

but the second I spin up a thread it seems to lock the app until the threading stuff is finished.

So my question - how SHOULD i trap this type of gps error stuff to provide the correct feedback?


Did you look at this API?

http://msdn.microsoft.com/en-us/library/system.device.location.geocoordinatewatcher.trystart(v=VS.92).aspx

Based on what it says here, you should be able to do something within the following framework:

public partial class MainPage : PhoneApplicationPage
{
    GeoCoordinateWatcher watcher;

    // Constructor
    public MainPage()
    {
        InitializeComponent();
        Loaded += (source, args) =>
        {
            watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High);

            watcher.PositionChanged += (sender, e) =>
            {
                // update the user
            };

            watcher.StatusChanged += (sender, e) =>
            {
                // update the user
            };

            if (!watcher.TryStart(false, TimeSpan.FromSeconds(5)))
            {
                // show the error somewhere
            }
        };
    }
}

Then, during execution of the app, if something goes wrong with the GPS (loses signal, etc) then you can respond appropriately inside the StatusChanged event handler.

Let me know if I'm off the mark here and I'll keep thinking about better solutions...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号