RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Take Your Apps Far and Wide with a GPS Tracking System  : Page 2

You already know that GPS is used extensively in a wide variety of mobile devices in order to track delivery and service vehicles—or any other mobile fleet. This kind of application is not as difficult as you may at first think. Find out how to use Visual Studio to create a GPS tracking app, with maps, that runs on Windows Mobile Pocket PC devices.

Understanding GPS Data
Generally, most GPS receivers support the NMEA (National Marine Electronics Association; see http://www.gpsinformation.org/dale/nmea.htm for more information) standard. Some common NMEA data sentences (the data output of the GPS receiver) are shown in Table 1.

Table 1. Some common NMEA data.

Sentence Description
$GPGGA Global positioning system fixed data
$GPGLL Geographic position - latitude / longitude
$GPGSA GNSS DOP and active satellites
$GPGSV GNSS satellites in view
$GPRMC Recommended minimum specific GNSS data
$GPVTG Course over ground and ground speed

For this project, I am mainly concerned with the geographical location, that is, latitude and longitude. Therefore, I only need to look out for sentences beginning with $GPGGA.

The breakdown of a typical $GPGGA sentence is shown in Table 2.

Table 2. The fields in a $GPGGA sentence.

Field Sample Description
0 $GPGGA Sentence Prefix
1 001431.092 UTC Time (in hhmmss.sss format)
2 0118.2653 Latitude (in ddmm.mmmm format)
3 N (N)orth or (S)outh
4 10351.1359 Longitude (in dddmm.mmmm format)
5 E (E)ast or (W)est
6 0 Position Fix (0 is invalid,1 is valid, 2 is Valid DGPS, 3 is Valid PPS)
7 04 Satellites used
8   Horizontal dilution of precision
9 -19.6 Altitude (unit specified in next field)
10 M M is Meter
11 4.1 Geoid separation (unit specified in next field)
12 M M is Meter
13   Age of DGPS data (in seconds)
14 0000 DGPS Station ID
15 *5B Checksum
15 CRLF Terminator

As you can see from Table 2, to obtain the latitude and longitude of a location you only need fields 2 and 4. One important point to note is that the latitude and longitude information are both represented in the "degrees, minutes, and decimal minutes" format—ddmm.mmmm. However, most mapping applications require longitude and latitude to be expressed in decimal degrees (dd.dddddd) with a corresponding sign (negative for south latitude and west longitude). Hence, you need to perform some conversions.

The following formula converts the ddmm.mmmm format to decimal degrees format:

dd.dddddd = (ddmm.mmmm \ 100) + _
           ((ddmm.mmmm - ((ddmm.mmmm \ 100) * 100)) / 60)
If the direction is "South," then the latitude must be negated. If the direction is "West," then the longitude must be negated.

One additional requirement for this application is to display the speed in which the truck is traveling. For this, you can examine all GPS data that is prefixed with the "$GPRMC" word. Here's an example of a sentence beginning with the "$GPRMC" word:

Figure 6. If you test the Pocket PC application at this point, it should look similar to this.
The speed is indicated in the eighth field and is expressed in knots (this information is available in the NMEA standard). In the example above, it is 000.05 knots. Converting from knots to kilometers per hour is easy—simply multiply it by 1.85.

With this knowledge of how to interpret the GPS data, you can now define the processGPSData() subroutine and extract the relevant information:

    Private Sub processGPSData(ByVal str As String)
        Dim rawLatLng As Double
            '---separate the GPS data into various fields---
            Dim field() As String
            field = str.Split(",")
            Dim lat, lng As Double

            Select Case field(0)
                Case "$GPGGA"
                    rawLatLng = Convert.ToDouble(field(2))
                    lat = (rawLatLng \ 100) + _
                         ((rawLatLng - ((rawLatLng \ 100) * 100)) / 60)

                    '---latitude is negative if South---
                    If field(3) = "S" Then
                        lat *= -1.0
                    End If

                    rawLatLng = Convert.ToDouble(field(4))
                    lng = (rawLatLng \ 100) + _
                         ((rawLatLng - ((rawLatLng \ 100) * 100)) / 60)

                    '---longitude is negative if West---
                    If field(5) = "W" Then
                        lng *= -1.0
                    End If

                    '---display the lat and lng---
                    lblLat.Text = "Lat:" & lat
                    lblLng.Text = "Lng:" & lng

                Case "$GPRMC"
                    '---display the speed---
                    If field(7) = String.Empty Then
                        lblSpeed.Text = "Speed: 0 km/h"
                        lblSpeed.Text = "Speed: " & _
                         (Convert.ToDouble(field(7)) * 1.85).ToString _
                         & " km/h"
                    End If
            End Select
            MsgBox("An error has occurred")
        End Try
    End Sub
The Disconnect menu item closes the serial port connection:

    '---Disconnect menu item---
    Private Sub MenuItem3_Click( _
       ByVal sender As System.Object, _
       ByVal e As System.EventArgs) _
       Handles MenuItem3.Click
        MenuItem1.Enabled = True  '---Connect GPS---
        MenuItem3.Enabled = False '---Disconnect---
    End Sub
The Server IP menu item allows you to set the IP address of the server (to be built later, in the second part of this article):

    '---Server IP menu item---
    Private Sub MenuItem4_Click( _
       ByVal sender As System.Object, _
       ByVal e As System.EventArgs) _
       Handles MenuItem4.Click
        ServerIP = InputBox( _
           "Please enter the IP address of server", "Server IP")
    End Sub
The ID menu item sets the identity of the user. This allows the server to differentiate between different users:

    '---ID menu item---
    Private Sub MenuItem5_Click( _
       ByVal sender As System.Object, _
       ByVal e As System.EventArgs) _
       Handles MenuItem5.Click
        ID = InputBox("Please enter your ID", "ID")
    End Sub
Testing the Pocket PC application
With the Windows Mobile device connected to the PC via ActiveSync, you can now deploy the application onto it (simply press F5 in Visual Studio 2005). When the application is loaded, tap on the Connect GPS menu item (see Figure 6). Assuming that the GPS receiver is paired correctly and that the serial port used is COM4, you will observe GPS data are streaming in continuously. You should also be able to observe the latitude and longitude of your position. If you try this inside a moving car, you will also observe that the speed changes as the car moves.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date