Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Simulating GPS position changes using Reactive Extensions in Windows Phone environment

DZone's Guide to

Simulating GPS position changes using Reactive Extensions in Windows Phone environment

· Mobile Zone
Free Resource

Download this comprehensive Mobile Testing Reference Guide to help prioritize which mobile devices and OSs to test against, brought to you in partnership with Sauce Labs.

It has become much easier to simulate GPS position change events having Location tool in Emulator, where you are able to pin positions on a map, but for some Unit Tests and mock services it is important to simulate GPS data programmatically. For this reason you would obviously use Reactive Extensions(Rx). Rx allows you to create Observable interface implementation based on Timer and attach it to position reading method. Bellow is the code I use to generate a GeoPosition with random Latitude and Longitude parameters. If isSimulation variable is set to true then simulation mode is activated, otherwise application uses standard GPS functionality.


private readonly GeoCoordinateWatcher _gcw = new GeoCoordinateWatcher();
 
private readonly Random _random = new Random();
 
public GpsSamplePage()
{
    InitializeComponent();
 
    bool isSimulation = true;
    if (!isSimulation)
    {
        _gcw.PositionChanged += (s, e) => GcwPositionChanged(e);
        _gcw.Start();
    }
    else
    {
        Observable.Timer(TimeSpan.FromSeconds(0), TimeSpan.FromSeconds(4))
        .Select(x =>
            new GeoPositionChangedEventArgs<GeoCoordinate>(
                new GeoPosition<GeoCoordinate>(DateTime.Now,
                    new GeoCoordinate()
                        {
                            // -90 <= latitude <= 90
                            Latitude = (_random.NextDouble() * 180.0) - 90.0,
                            // -180 <= longitude <= 180
                            Longitude = (_random.NextDouble() * 360.0) - 180.0
                        }
 
                ))).ObserveOnDispatcher().Subscribe(GcwPositionChanged);
    }
}
 
void GcwPositionChanged(GeoPositionChangedEventArgs<GeoCoordinate> e)
{
    Dispatcher.BeginInvoke(() =>
    {
        // thread safe
        tbLatitude.Text = e.Position.Location.Latitude.ToString();
        tbLongitude.Text = e.Position.Location.Longitude.ToString();
    });
}

Source: http://www.eugenedotnet.com/2011/11/simulating-gps-position-changes-using-reactive-extensions-in-windows-phone-environment/

Analysts agree that a mix of emulators/simulators and real devices are necessary to optimize your mobile app testing - learn more in this white paper, brought to you in partnership with Sauce Labs.

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}