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
- 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:
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).
- 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
- 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:
In our discussion, we will look at GPSDemo.cs first followed by examining the implementation
of the GPS support functions.
- GPSDemo.cs: implements the main XNA framework functions that supports user
- GPSDemo_Compute.cs: implements all GPS specific 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)
Label E: GPS support variables
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
Label F: Support for converting Geo Reading to Pixels
The above listing shows variables for working with GPS. We observe four interesting
- GeoCooridnateWatcher: this is thread that is capable of samping the GPS receiver
(much like the accelerometer sampling
thread we have discussed).
- GeoCoordinate: The Watcher returns information encapsulated in this data type, information
returned include the longitude, latitude, altitude (in doubles for accuracy), etc.
- GeoPositionStatus: Indicates the GPS receiver status.
- CivicAddressResolver: Capable of returning civic address based on GeoCoordinate
reading. This functionality is not supported in current WP7 API.
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:
The Update() Function:
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
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.
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
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).
Once again, in this demo program GeoCoordinate to pixel conversion is based on one
single floating point conversion factor!
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().
This is the status update function for "Watcher Status Change Event" (called
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.
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
- Label Z1: Deciding if new position markers should be drawn to indicate movement.
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.
Computing and Software Systems
University of Washington, Bothell
Project home page:
The Game-Themed Introductory Programming Project.
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