Most of the new Windows Mobile devices include a GPS receiver as part of the standard configuration. However, one problem is that of the repeated "cold start." Presumably to save battery life, the GPS receiver is turned off when it is not being used. Unlike standard GPS devices, mobile GPS chipsets do not save data when they are powered off, requiring a "cold start" each time they are used. This means up to 10 minutes of keeping the phone motionless until it has locked on to the satellites. Windows Mobile 5 and 6 standard / smartphone editions do not provide user-accessible configuration options to change this.
However, if the GPS remains turned on, even after losing its fix (i.e. by going inside) it will be able to re-acquire its location within seconds of being placed in an area that has a signal. Also, once locked on to a signal, the receiver is able to hang onto it even when going into areas where it would not be able to lock on to the signal from a cold start.
I originally dealt with this annoyance by leaving Google Maps running all the time in the background. This solution was imperfect, since it used a lot of memory and CPU, as well as downloading data from the Internet to update the map, which is quite expensive on many mobile devices. I instead designed this utility to run in the background, keep the GPS open, and poll its status at a user-defined interval.
This program is also useful if you want to quickly test your GPS to make sure it is configured correctly and/or has a signal.
How to Use the Microsoft Intermediate GPS Driver
The library used in this app is an open source sample provided for free with the Windows Mobile 6 standard SDK. It encapsulates the API hooks, allowing for quick and easy access to the phone's GPS using C# managed code.
I have included the necessary source for the library; if you have the Windows Mobile 6 Standard SDK, those files can also be found at "\program files\Windows Mobile 6 SDK\Samples\Smartphone\CS\GPS".
Add the whole folder to your project (minus the demo app), and add...
... (or the VB equivalent) to your classes that require GPS access.
IMPORTANT: Known Issues with the Microsoft .NET Compact GPS Library
One of the reasons that unnecessarily complex solutions have been posted here and elsewhere is, THE WINDOWS MOBILE 6 SDK LIBRARY DOES NOT WORK PROPERLY IN THE WINDOWS MOBILE 6 EMULATOR. HOWEVER, IT WORKS PERFECTLY ON AN ACTUAL PHONE. A bunch of NMEA files are included with the SDK to simulate navigation on the emulator... when used with the "FakeGPS" driver (for testing GPS apps in the emulator) the latitude and longitude are alternately invalid or ridiculous (near the South Pole.) There is an MSDN blog somewhere apologizing for this goof and giving a possible fix (an equation to convert decimal degrees to standard latitude-longitude, which does absolutely nothing to solve the problem.)
That said, use the library. It makes getting GPS data easier than opening a text file, as you will see in the code below. The problem is with the emulator. My advice would be, in the emulator, test with simulated latitude and longitude fed in however you see fit (i.e. an array of values, text file, etc.), then wire your position-getter methods up to the library and deploy to an actual GPS-enabled device. It will function as expected.
The project that accompanies this article, while small and simple, demonstrates the use of the core methods that you will actually use when building GPS-enabled applications:
Start the GPS:
Gps g = new Gps(); Gps.Open();
Determine if the GPS is ready and knows its location:
Get latitude / longitude:
double latitude = g.GetPosition().Latitude;
double longitude = g.GetPosition().Longitude;
Stop the GPS:
Knowing all of this, one can easily build a decent application, ready for beta testing by normal users, in under 4 hours.
Using the Code
The code for this app is contained in "form1.cs" and its associated files. The other source files bundled with the project are the GPS objects provided by Microsoft.
It is so simple as to be trivial - the entire application runs in the code behind
form1, the main window.
When the program starts, the user is given an option to turn on the GPS and start polling it at regular intervals. Here is how the device is stopped and started:
public bool isTurnedOn = false;
public int pollInterval = 5;
public Gps gps;
private void Form1_Load(object sender, EventArgs e)
gps = new Gps();
private void mnuTurnOn_Click(object sender, EventArgs e)
isTurnedOn = true;
mnuTurnOn.Text = "Turn Off";
timer1.Interval = pollInterval * 60 * 1000;
timer1.Enabled = true;
catch (Exception ex)
MessageBox.Show("Error: could not find GPS device");
isTurnedOn = false;
mnuTurnOn.Text = "Turn On";
timer1.Enabled = false;
Accessing the GPS location data is almost too easy. Here is the
UpdateStatus() method that is the heart of the program that checks if the GPS is locked to a sufficient number of satellites, and then gets its latitude and longitude.
private void UpdateStatus()
lblState.Text = "GPS Turned Off";
label1.Visible = label2.Visible = lblStatus.Visible =
lblLastFix.Visible = lblLastUpdate.Visible = false;
lblState.Text = "GPS is turned on.";
label1.Visible = label2.Visible = lblStatus.Visible =
lblLastFix.Visible = lblLastUpdate.Visible = true;
lblLastUpdate.Text = DateTime.Now.ToString();
lblLastFix.Text = "Locked on to satellites:
" - "+gps.GetPosition().Longitude.ToString();
lblLastFix.Text = "No signal";
How to Test in the Emulator
Seeing as the "FakeGPS" emulation does not work, on-device testing should be started early for any application built using these libraries. Building the application here that I have posted for download results in an EXE and a DLL. Place both in the same folder on your mobile device, and you should be good to go.
Unfortunately most devices are shipped without the newer versions of the .NET Compact Framework. If you have the Windows Mobile SDK, there will be a CAB for each different processor. Don't worry about breaking your phone by choosing the wrong one - it will simply refuse to install. You can also download the framework by itself from Microsoft.
Deploying to the Device
The project is currently set to use a Windows Mobile 6 standard build target (smartphones such as the Motorola Q9H or Samsung Jack II. It will also build for Windows Mobile 6 professional (touchscreen PDAs / PocketPCs.)
It has been tested in the real world on a Motorola Q9H with standard configuration but should work on any of the above mentioned device categories provided that a GPS chipset is present and configured properly.
Windows Mobile 5 devices should also accept this, and other applications using the intermediate GPS driver. Switch the build target in Visual Studio, and look for warnings on compile: if you are using components (mainly UI ones) not allowed on 5 (i.e. an embedded web browser control that can be manipulated by the host application) you will be notified at build time, and can make the appropriate (minor) changes.
Obviously, to make a production GPS application using .NET CF, you will want to create a Setup and Deployment project and explicitly specify that the .NET Compact Framework 2.0 or 3.5 be included with it, rather than making the user download it separately. The current build configuration is for 3.5, but it builds perfectly to a 2.0 target as well without modifications.
- 18th July, 2008: Initial post