DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
11 Monitoring and Observability Tools for 2023
Learn more

Simulating GPS position changes using Reactive Extensions in Windows Phone environment

Jevgeni Tšaikin user avatar by
Jevgeni Tšaikin
·
Nov. 18, 11 · Interview
Like (0)
Save
Tweet
Share
5.46K Views

Join the DZone community and get the full member experience.

Join For Free
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/
Windows Phone

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Test Design Guidelines for Your CI/CD Pipeline
  • Using GPT-3 in Our Applications
  • How To Best Use Java Records as DTOs in Spring Boot 3
  • Real-Time Analytics for IoT

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: