Export (0) Print
Expand All

Using the GPS Intermediate Driver from Managed Code

Windows Mobile 6.5
4/19/2010

The GPS Intermediate Driver is a software layer that sits between applications and the device driver for GPS hardware. This layer of abstraction allows applications to be written once and work with multiple GPS devices. The GPS Intermediate Driver API is exposed through a native code library. You can gain access to this library from managed code by using the sample that is included with the Windows Mobile Professional SDK. By default, this sample is installed at the following location:

C:\Program Files\Windows Mobile 6.5.3 DTK\Samples\PocketPC\CS\GPS

The GPS sample project included in the SDK is a managed wrapper for the GPS Intermediate Driver that uses pInvoke calls to access the native library. When creating your own managed GPS application, compile the sample project and add a reference to the output assembly to your project. Then add the following using directive to your code.

using Microsoft.WindowsMobile.Samples.Location;
Bb158708.note(en-us,MSDN.10).gifNote:
The Windows Mobile Version 5.0 SDK shipped with a preliminary version of the managed GPS sample. You should install the Windows Mobile 6.5.3 DTK and use the GPS sample from that installation, even if you are targeting Windows Mobile Version 5.0-based devices.

Once you have added a reference to the sample assembly to your application, you can access the GPS device through the Gps object. The GpsDeviceState and GpsPosition helper objects store information about the GPS device and your current location respectively.

Gps gps = new Gps();
GpsDeviceState device = null;
GpsPosition position = null;

Add event handlers for the DeviceStateChanged and LocationChanged events of the Gps object so that your application is alerted when either of the device status or your location data is updated by the device.

updateDataHandler = new System.EventHandler(UpdateData);
gps.DeviceStateChanged += new Microsoft.WindowsMobile.Samples.
Location.DeviceStateChangedEventHandler(gps_DeviceStateChanged);
gps.LocationChanged += new Microsoft.WindowsMobile.Samples.
Location.LocationChangedEventHandler(gps_LocationChanged);

When you implement these event handlers, you should use Invoke to trigger an event in your application’s thread rather than holding up the GPS device’s thread. The following example gets the GPS position information from the LocationChangedEventArgs and then invokes the event handler updateDataHandler that is defined elsewhere in the application.

protected void gps_LocationChanged(object sender, 
                                    LocationChangedEventArgs args)
{
  position = args.Position;
  Invoke(updateDataHandler);
}

In your application’s event handler for GPS data, you use the GpsDeviceState and GpsPosition helper objects to access the data.

void UpdateData(object sender, System.EventArgs args)
{
  if (gps.Opened)
  {

    // verify that the device object is not null
    if (device != null)
    {
       // display device status
       StatusLabel.Text = device.FriendlyName + "Status: " +
       device.ServiceState + ", " + device.DeviceState;
    }

    // verify that the position object is not null
    if (position != null)
    {
      //The position object exposes
      // additional properties that indicate 
      //which properties are currently valid.
      if (position.LatitudeValid)
      {
        // display latitude
        LatitudeLabel.Text = "Latitude(D,M,S): " + 
        position.LatitudeInDegreesMinutesSeconds;
      }
      …

Community Additions

ADD
Show:
© 2014 Microsoft