Skip to main content
January 28, 2011
Windows Phone Developer Blog

Windows Phone GPS Emulator



All Windows Phone devices have a built-in Assisted GPS (aGPS), which is used by various phone applications including maps, camera, and search (to provide location-based search results). Developers can access location information on Windows Phone by using the System.Device.Location namespace, which is supported in .NET 4 and later. The GeoCoordinateWatcher class supplies location data based on latitude and longitude coordinates.

Working with the GeoCoordinateWatcher is relatively simple. Later in this piece, we’ll explain in more detail how to work with that class and how to test your application on a Windows Phone 7. However, sometimes your application requires more than just a single location, it requires tracking movement, and you may need to test how your application behaves in different locations.

At these times, it may look odd to be driving around the block with your Windows Phone attached to a laptop while you try to debug your application.

Don’t worry—you’re in good hands. The Windows Phone GPS Emulator (a small WPF application) and one WP7 DLL enable you to debug your application on the Windows Phone emulator or a real device without leaving the comfort of your home or office. Once you’ve completed your testing and debugging, you only need to change a single line of code to switch to the device back to real GPS.

With the GPS Emulator, you can set a location anywhere on the globe by using the map display. Furthermore, you can plan routes with multiple intermediate waypoints, or use Bing services to calculate driving directions between locations. Once you’ve planned a route, you can simulate driving through the pre-defined waypoints along the path.

The recipe includes:

  • The Windows GPS Emulator application
  • The Windows Phone GPS Emulator Client DLL
  • A simple Windows Phone Test client
  • A complete end-to-end Windows Phone App using Bings maps (a more complex sample)

Using the GPS Emulator lets you create complex path that you can playback just as if you were driving or walking. Then, you can choose your Windows Phone application and receive the location information form the GPS Emulator just as if you got it via the real GPS.

image

 

Using GPS Emulator with your Windows Phone Application

But first thing first, let’s figure out how does the GPS Emulator helps you debug your Windows Phone client application?

When you download the Windows Phone GPS Emulator recipe, you’ll notice there is an assembly called GPSEmulatorClient. This DLL has a class called GeoCoordinateWatcher, which is the same name as the System.Device.Location.GeoCoordinateWatcher class, just in a different name space. This homebrew, “fake” GeoCoordinateWatcher class implements the IGeoPositionWatcher interface, which is the same interface that the “real” System.Device.Location.GeoCoordinateWatcher class implements. Therefore, we can say that the GPSEmulatorClient.GeoCoorinateWatcher implements the same API as the real System.Device.Location.GeoCoordinateWatcher. This means that both classes are transparent to the developer in terms using the location APIs. We added all the functions that are not defined by the IGeoPositionWatcher interface but are public in the System.Device.Location.GeoCoordinateWatcher class. As a result, you can write your application to use the GeoCoordinateWatcher and only change the initiation process based on your environment. You’ll need to add a reference to the GpsEmulatorClient as well as the using GpsEmulatorClient; statement.

In the GpsEmulatorPhoneTestClient application, we define the symbol by using the #define keyword. Next, in the MainPage constructor, we use the #if GPS_EMULATOR statement to distinguish between our testing environment and a real device, as shown in the next code snippet:

// This line has to be the first line in the file
#define GPS_EMULATOR // defining a compiler GPS symbol. 
 
// Init
IGeoPositionWatcher<GeoCoordinate> _Watcher;
#if GPS_EMULATOR
            _Watcher = new GpsEmulatorClient.GeoCoordinateWatcher();
#else
            _Watcher = new System.Device.Location.GeoCoordinateWatcher();
#endif

Since _Watcher is of type IGeoPositionWatcher<GeoCoordinate> both implementations—the real and the fake—can be casted to that variable. From this point on, it is simply a matter of using the GeoCoordinateWatcher method and properties to listen to location and status changes.

Working with the GeoCoordinateWatcher

As we said, the GeoCoordinateWatcher class is part of the System.Device.Location namespace. The GeoCoordinateWatcher class has the following properties:

  • DesiredAccurecy is of type GpsPositionAccurecy, which can be Default or High. High means the GPS readings are of high resolution, providing more accurate location (down to a few meters where possible). Note that High location resolution can shorten battery life.

    Note: One of the GeoCoordinateWatcher constructors has an input of GpsPositionAccuracy. By default it is set to Default, which means lower location resolution.

  • Permission is of type GeoPositionPermission, which defines the application’s level of access to the device. With Windows Phone you need to declare in your application manifest that you want to use the GPS device.
  • Position is of type GeoCoordinate and holds the current location, if the GPS status is valid.
  • Status is of type GpsPositionStatus, which indicates if the GPS readings are valid

There is one more property, MovementThreshold, which defines the minimum movement threshold, which by default is set to zero. You need to know that the GPS fires a lot of events, about one per second. That may not seem like a lot, but if you don’t move that fast or if your application doesn’t need to keep track at such a high resolution, make sure you set MovementThreshold to something other than zero. You can read more about this in a post by Jaime Rodriguez.

Next, you’ll want to register to the following events:

  • PositionChanged occurs when the location service detects a change in position, taking into account limitations like MovementThreshold
  • StatusChanged occurs when the status of the location service changes from Initializing to Ready, for example.

There’s not a lot more to it besides handling the events and updating your application accordingly. In our test sample, we simply read the values and print them on a screen, as shown in the next code snippet and image.

void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
     tbTimeAcquired.Text = e.Position.Timestamp.ToString();
     tbLatitude.Text = e.Position.Location.Latitude.ToString();
     tbLongtitude.Text = e.Position.Location.Longitude.ToString();
}

As you can see, the GeoPositionChangedEventArgs holds the position relevant to the location reading that triggered the PositionChanged event. The Position, which is of type GeoCoordinate class, includes, among other properties, Lat, Long, and time. It can include additional information, including VerticalAccuracy, altitude, speed, and more.

The output of our client application looks something like this:

image

The purple circle represents the current location transmitted by the Windows Phone GPS Emulator. It turns out that Microsoft building 24, where my office is located, is at Lat 47.6416 and Long -122.1306.

Follow a Few Rules

Before we dive into the technical details of the GPS Emulator, there are few rules you need to follow:

  • Run the GPS Emulator in Admin mode; it opens a WCF channel through which the Windows Phone application can read the location emulation.
  • Make sure the GPS Emulator runs before calling the GPSEmulatorClient.GeoCoordinateWatcher.Start method, as none of the demos really handles this scenario.
  • You’ll need to create a Bing Maps Account to fully access the power of the GPS Emulator. You can create an account here: http://www.bingmapsportal.com.
  • Remember to remove the #define GPS_EMULATOR symbol when testing on a real device.

The Windows Phone GPS Emulator is a great tool for testing your location-based Windows Phone applications without leaving the comfort of your home or office. But be sure you test your application on a real device, as location-based info or the lack of it could result in interesting edge cases in your application.

The Windows Phone GPS Emulator recipe includes detailed documentation including few samples and explanation on how the GPS emulator works.

Follow Windows Phone announcements on Twitter at WP7DEV

Follow Yochay on Twitter

Get started with free tools and free training