|Windows Phone 7 Sensors and XNA|
|GPS: XNA GPS Demo|
|Back to main page||
Release 1.0 (XNA V4.0)
Pre-requisite: this tutorial assumes you have read through the Pinch Zoom Touch Panel Tutorial, in particular,
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:
The following discuss the implementation details.
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 F blocks.
Label F: Support for converting Geo Reading to Pixels
The above listing shows variables for working with GPS. We observe four interesting variable types:
- 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 with "Start()".
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.
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).
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 from above).
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:
|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.|