Go to Faculty Home /   Curriculum Resources /  Windows Phone 7 Sensors and XNA /  GPS

Windows Phone 7 Sensors and XNA

GPS: XNA GPS Demo

Release 1.0 (XNA V4.0) 9/15/2010


1. Topics Covered

This program demonstrates XNA support for working with the GPS on WP7. You may wish to download the source code, open the project and refer to the source code while follow the discussion. Topics covered in this tutorial includes:
  • Programming with GPS Receiver

Pre-requisite: this tutorial assumes you have read through the Pinch Zoom Touch Panel Tutorial, in particular,


2. Demo Program Behavior

Start the application on the phone, and you will see the following screen:
Portrait Image

Wait for GPS Status to show "Ready". Now, you can travel (walk or by car) around to see the center compass object follow your movement accordingly. Drag your finger on the phone to change the GPS resolution: drag in the negative x-direction to increase GPS resolution (screen space represents smaller area, e.g., the entire screen width represents 10 meter distance); drag in the positive direction to decrease GPS resolution (e.g., the entire screen width represents 10,000 meters). NOTE:

  1. The actual GPS receiver's accuracy is less than 3 meters. This means, if your movement is less than 3 meters, the GPS literally cannot distinguish if you have moved. This demo program lets you set the GPS accuracy such that the entire screen width represents 3 meters physical world distance. It is important to note that such high accuracy would require a device that can resolve centimeter movements. As a general rule of thumb, if your accuracy setting is such that the screen width represents movements of less than 50 meters then you are probably dealing with mostly noise!
  2. The distance and accuracy reported by this program are based on scientifically proven inaccurate rough estimations!! One should never use any of the readings for any applications.

The following discuss the implementation details.


3. Solution Structure and External Environment

  • Source code files: The implementation are contain in two files:
    • GPSDemo.cs: implements the main XNA framework functions that supports user interaction.
    • GPSDemo_Compute.cs: implements all GPS specific functions.
    In our discussion, we will look at GPSDemo.cs first followed by examining the implementation of the GPS support functions.
     
  • External Resources: there are the usual background music and sound effect. In this case, the only other objects are the background (Landscape.png) and the compass images (Compass.png).

4. Implementation Details:

4a. GPSDemo.cs: (User Interaction Support)

An Image Image
The above listing shows a structure similar to that from the structure of the Pinch Zoom Touch panel tutorial. There main differences are the extra instance variables for supporting GPS operations at Labels E, F, and G. The variables under Label G are for general support of output movement markers (if distance travel is grater than 2 pixels, output a red markers to indciate your movement path). These variables are not directly related to GPS functionality and will not be examined in detailed. In the following, we examine each of  E and F blocks.
Label E: GPS support variables
An Image Image
The above listing shows variables for working with GPS. We observe four interesting variable types:
  1. GeoCooridnateWatcher: this is thread that is capable of samping the GPS receiver (much like the accelerometer sampling thread we have discussed).
  2. GeoCoordinate: The Watcher returns information encapsulated in this data type, information returned include the longitude, latitude, altitude (in doubles for accuracy), etc.
  3. GeoPositionStatus: Indicates the GPS receiver status.
  4. CivicAddressResolver: Capable of returning civic address based on GeoCoordinate reading. This functionality is not supported in current WP7 API.
Label F: Support for converting Geo Reading to Pixels
An Image Image
The above listing is to show that the conversion from GeoCoordinate reading to meters/pixels is done in a extremely non-scientific way. Once again, please do not rely on the readings from this program for any real purposes.

The Initialize() function:

An Image Image
The above listing shows the the instantiation of the GPS watcher, registration of position change, and status change event handlers. The GPS receiver sampling begins with "Start()".
The Update() Function:
An Image Image
The only functionality required in update is to support the user's dragging of finger to increase/decrease the GPS accuracy. The movement of the compass is caused by the position change events from the GPS and will be computed in the GPS position change event service routine.
  • Label U1: decoding the user gesture.
    An Image Image
    The above listing is from Label U1 in the Update() function. Once again,  see that GPS accuracy is grossly approximated by a linear conversion factor defined simply by the mScreenWidthRepresent variable (in InitGeo() data).

4b. GPSDemo_Compute.cs: Supports GPS functionality

An Image Image
This file implements the all GPS specific functionality. The following explains each function in this file according to their order in the above listing. All except InitGeo() function, where all variables are reset (e.g., resetting number of position markers to zero) and ComputeGeoDeltaToPixelFactor() function is called to compute the factor that converts GeoCoordinate to pixels (refer to the following).
ComputeGeoDeltaToPixelFactor()
An Image Image
Once again, in this demo program GeoCoordinate to pixel conversion is based on one single floating point conversion factor!
WatcherStatusChange()
An Image Image
Event service routine for watch status change. Remember, asynchronous event service functions always come in pairs. The actual service function (this function) is called asynchronously and should not change any instance variables. Instead, a thread-safe function call is made (via Lamda), in this case: StatusChange().
StatusChange()
An Image Image
This is the status update function for "Watcher Status Change Event" (called from above).
WatcherPositionChanged()
An Image Image
Event service routine for "Watcher Position Change event". This event is triggered when the GPS detect the phone has changed its position, typically, a movement of more than 3 meters.
PositionChanged()
An Image Image
This routine is called in a thread-safe manner form the above. This routine updates the position of the compass object. The compass object is assumed to have an initial position of center of the application screen. After initialization, at Label Z, GeoPosition changes are converted to pixel changes according to the linear conversion factor. And when the movement involves significant pixels distances (e.g., > 2 pixel), a marker is drawn to indicate movement. The following two listings shows the details inside Label Z:
  • Label Z: Compute GeoPosition changes and convert to pixel movements
    An Image Image
  • Label Z1: Deciding if new position markers should be drawn to indicate movement.
    An Image Image

5. Self Test and Exercises

  • What is the proper way of converting GeoPosition changes to physical distances? Answer, well, this is a non-trivial problem. NOAA has a calculator that helps convert differences in Latitude/Longitude into distances (http://www.nhc.noaa.gov/gccalc.shtml, from this site, you can find references on how to approach such a computation).
  • Practice Project: Support changing of GPS accuracy via Pinch Zoom.
  • Practice Project: allow user to drag the compass to any position on the screen and the application will print out the approximated distance between that position and the user's current location.

References


Kelvin Sung
Computing and Software Systems
University of Washington, Bothell
ksung@u.washington.edu
Project home page: The Game-Themed Introductory Programming Project.

Microsoft Logo This work is supported in part by Microsoft Research under the Computer Gaming Curriculum in Computer Science RFP, Award Number 15871 and 16531, and Microsoft Higher Education.
9/15/2010
| | |
Microsoft