twitter rodrigue_ linkedin rodrigue hajjar

WP7: Rx and GeoCoordinateWatcher

Tags: WP7

This isn’t a full tutorial but more like a snippet, I’m writing about this because its not the first time someone asked about: what would be a nice fast way to get a geocoordinate position then stopping it while skipping the firsts and not writing a lot of lines of codes.

So here it is, first add the Microsoft.Phone.Reactive and System.Observable assemblies.

Then copy paste this line of code or even insert it in a snippet and voila!

GeoCoordinateWatcher watcher = new GeoCoordinateWatcher();
Observable.FromEvent<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher, "PositionChanged").Skip(3).Subscribe((arg) =>
{
    watcher.Stop();
    //do whatever you want with the positions
    Debug.WriteLine(arg.EventArgs.Position.Location.Latitude + arg.EventArgs.Position.Location.Longitude);
});
watcher.Start();

Basically what happens is: the observable subscribes to the “PositionChanged” event of the watcher which is of type “GeoCoordinateWatcher”, skips the first 3 results and gets the ones after.
Once we’re in the event, (means we skipped the first 3 results); the watcher is stopped and we do whatever we want Wink

Add a Comment