GPS Intermediate Driver – Retrieving Location Information inside a Managed Application
Location Awareness becomes more and more important for many applications that are running on Windows Mobile Devices. Thanks to the fact that both Windows Mobile 5.0 and Windows Mobile 6 devices contain the GPS Intermediate Driver (GPSID), it is very easy for you as a developer to retrieve location information through GPS. With more and more devices having a built-in GPS receiver, it is about time that you are going to take advantage of this additional functionality as well. In this blog entry, I want to show you how easy it is to retrieve latitude and longitude information inside a Windows Mobile managed application, using C# and the .NET Compact Framework 3.5.
When you install either the Windows Mobile 5.0 or the Windows Mobile 6 SDK’s, you will get lots of sample code. One of the samples not only shows you how to make use of GPSID, but it also contains a managed wrapper around GPSID functionality. You can find these samples in the following folder:
<Installation Folder>\<Windows Mobile SDK>\Samples\PocketPC\Cs\Gps
or
<Installation Folder>\<Windows Mobile SDK>\Samples\Smartphone\Cs\Gps
Before you are able to use the managed wrapper around GPSID you need to build the GPS solution that is stored in the above-listed folders. Once you have built the GPS solution, you can make use of the functionality in the Microsoft.WindowsMobile.Samples.Location assembly. In order to do so, you need to import this assembly into your own solution.
This little sample application retrieves location information through GPSID and through the managed wrapper around GPSID. Each time the user selects the ‘Get LatLong’ menu entry, the latitude and longitude data is refreshed. To retrieve location information in this application, I am making use of the synchronous method GetPosition that is part of the Gps type. It is also possible to subscribe to a LocationChanged event, which is fired each time the GPS receiver gets updated location information. By making use of the LocationChanged event, you will receive position data in an asynchronous way. In a future blog entry I will show you how to make use of the LocationChanged event, but for right now I want my code to be small and simple, to make it easier to understand how to retrieve GPS data.
In order to use GPS functionality inside my application, the first thing I do is declare an instance variable of type Gps inside my Form class:
private Gps gps;
Before being able to retrieve location information from the GPS receiver, I need to create a new object of type Gps and call the Open method on that object. This operation activates the GPS hardware if my application is the first application making use of the GPS hardware. Once I am done using the GPS hardware, I need to call the Close method on the Gps object in order to switch off the GPS hardware if my application is the last one using it. In this way I make sure to preserve battery power when I no longer need GPS functionality on my device. So here is the code that opens and closes the connection to the GPS hardware:
private void menuGPSEnable_Click(object sender, EventArgs e)
{
if (gps == null)
{
gps = new Gps();
gps.Open();
menuLatLong.Enabled = true;
}
else
{
menuLatLong.Enabled = false;
gps.Close();
gps = null;
}
}
Retrieving location information in a synchronous way is only a matter of calling the GetPosition method inside the Gps object. Location information is returned in a GpsPosition type, containing a large number of properties with all kinds of GPS data.
In this simple application the only thing I am showing is latitude and longitude data, and only when that data is valid, which is indicated by another set of properties. Here is the code to retrieve latitude and longitude information:
private void menuLatLong_Click(object sender, EventArgs e)
{
GpsPosition position = gps.GetPosition();
if (position.LatitudeValid)
labelLatitude.Text = position.Latitude.ToString();
if (position.LongitudeValid)
labelLongitude.Text = position.Longitude.ToString();
}
Finally there is one more thing I need to do in this sample application. If the user is terminating the application when I still have an open GPS connection, I will make sure to close that connection.
private void menuExit_Click(object sender, EventArgs e)
{
if (gps != null)
gps.Close();
this.Close();
}
And that is all there is to make use of GPSID to retrieve location information in a consistent way without having to worry about the underlying GPS hardware. In one of my next blog entries I will tell you how you can test your location aware applications without even needing physical GPS hardware.
Happy Coding!
Constanze