hits counter

Using The GeoCoordinateReactiveService

Having created an Rx wrapper over the GeoCoordinateWatcher on a previous post, in this post I’ll demonstrate how it can be used in a simple application.

The application will display the status of the service, the position and the distance traveled.

For this simple application the service will be exposed as a singleton property of the App class:

public partial class App : Application
{
    // ...
</span><span style="background: white; color: blue">public static </span><span style="background: white; color: #2b91af">IGeoCoordinateReactiveService </span><span style="background: white; color: black">GeoCoordinateService { </span><span style="background: white; color: blue">get</span><span style="background: white; color: black">; </span><span style="background: white; color: blue">private set</span><span style="background: white; color: black">; }

public App() { // ...

    </span><span style="background: white; color: black">InitializePhoneApplication();

    </span><span style="background: white; color: green">// ...</span><span style="background: white; color: black">
}

</span><span style="background: white; color: green">// ...

</span><span style="background: white; color: blue">private void </span><span style="background: white; color: black">InitializePhoneApplication()
{
    </span><span style="background: white; color: green">// ...</span><span style="background: white; color: black">

    GeoCoordinateService = </span><span style="background: white; color: blue">new </span><span style="background: white; color: #2b91af">GeoCoordinateReactiveService</span><span style="background: white; color: black">();

    </span><span style="background: white; color: green">// ...</span><span style="background: white; color: black">

}

</span><span style="background: white; color: green">// ...

}

Getting the status of the service is very simple. It just requires subscribing to the StatusObservable. Since we want to display the status, we need to observe it on the dispatcher before:

App.GeoCoordinateService.StatusObservable
    .ObserveOnDispatcher()
    .Subscribe(this.OnStatusChanged);

For the position we do the same with the PositionObservable:

App.GeoCoordinateService.PositionObservable
    .ObserveOnDispatcher()
    .Subscribe(this.OnPositionChanged);

The distance traveled would seem a bit more complicated because we need to keep track of the last position and calculate the distance traveled on every position change. But this is where the Rx excels with its query operators. If we combine the position observable with the position observable having skipped one position with the zip operator we end up with an observable with the current and previous position. And if we apply a selector, we get the traveled distance:

App.GeoCoordinateService.PositionObservable
    .Zip(
        App.GeoCoordinateService.PositionObservable.Skip(1),
        (p1, p2) => p1.Location.GetDistanceTo(p2.Location))
    .ObserveOnDispatcher()
    .Subscribe(this.OnDistanceChanged);

You can find the complete implementation of the service and application here.


Resources:

No Comments