Click here to Skip to main content
15,880,608 members
Articles / Programming Languages / Visual Basic

Mapping with a GPS and VB.NET

Rate me:
Please Sign up or sign in to vote.
4.73/5 (16 votes)
22 Jan 2008CPOL7 min read 184.7K   12.2K   87  
An article on mapping with a GPS in VB.NET
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports System.IO.Ports


Public Class frmPP


#Region "Member Variables"

    ' Local variables used to hold the present
    ' position as latitude and longitude
    Public Latitude As String
    Public Longitude As String

#End Region



#Region "Constructor"


    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Try to open the serial port
        Try
            SerialPort1.Open()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            timer1.Enabled = False
            btnUpdate.Text = "Update"
            Return
        End Try

    End Sub


#End Region


    Private Sub timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles timer1.Tick

        If SerialPort1.IsOpen Then

            Dim data As String = SerialPort1.ReadExisting()
            Dim strArr() As String = data.Split("$")
            Dim i As Integer = 0

            If strArr.Length > 1 Then

                Try

                    For i = 0 To strArr.Length

                        Dim strTemp As String = strArr(i)
                        Dim lineArr() As String = strTemp.Split(",")

                        If (lineArr(0) = "GPGGA") Then

                            Try

                                ' Latitude
                                Dim dLat As Double = Convert.ToDouble(lineArr(2))
                                dLat = dLat / 100
                                Dim lat() As String = dLat.ToString().Split(".")
                                Latitude = lineArr(3).ToString() + lat(0).ToString() + _
                                "." + ((Convert.ToDouble(lat(1)) / 60)).ToString("#####")

                                ' Longitude
                                Dim dLon As Double = Convert.ToDouble(lineArr(4))
                                dLon = dLon / 100
                                Dim lon() As String = dLon.ToString().Split(".")
                                Longitude = lineArr(5).ToString() + lon(0).ToString() + _
                                "." + ((Convert.ToDouble(lon(1)) / 60)).ToString("#####")

                                ' Display
                                txtLat.Text = Latitude
                                txtLong.Text = Longitude

                                btnMapIt.Enabled = True

                            Catch

                                ' Can't Read GPS values
                                txtLat.Text = "GPS Unavailable"
                                txtLong.Text = "GPS Unavailable"
                                btnMapIt.Enabled = False

                            End Try

                        End If

                    Next

                Catch
                    'do nothing
                End Try

            End If

        Else

            txtLat.Text = "COM Port Closed"
            txtLong.Text = "COM Port Closed"
            btnMapIt.Enabled = False

        End If

    End Sub



    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click

        ' cycle timer
        If timer1.Enabled = True Then
            timer1.Enabled = False
        Else
            timer1.Enabled = True
        End If

        ' update button label
        If btnUpdate.Text = "Update" Then
            btnUpdate.Text = "Stop Updates"
        Else
            btnUpdate.Text = "Update"
        End If

    End Sub


    Private Sub exitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitToolStripMenuItem.Click

        Application.Exit()

    End Sub


    Private Sub toolStripMenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles toolStripMenuItem2.Click

        Try
            SerialPort1.Close()
            SerialPort1.PortName = "COM1"
            SerialPort1.Open()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "COM1")
        End Try

    End Sub



    Private Sub toolStripMenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles toolStripMenuItem3.Click

        Try
            SerialPort1.Close()
            SerialPort1.PortName = "COM2"
            SerialPort1.Open()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "COM2")
        End Try

    End Sub



    Private Sub toolStripMenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles toolStripMenuItem4.Click

        Try
            SerialPort1.Close()
            SerialPort1.PortName = "COM3"
            SerialPort1.Open()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "COM3")
        End Try

    End Sub



    Private Sub toolStripMenuItem5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles toolStripMenuItem5.Click

        Try
            SerialPort1.Close()
            SerialPort1.PortName = "COM4"
            SerialPort1.Open()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "COM4")
        End Try

    End Sub



    Private Sub toolStripMenuItem6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles toolStripMenuItem6.Click

        Try
            SerialPort1.Close()
            SerialPort1.PortName = "COM5"
            SerialPort1.Open()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "COM5")
        End Try

    End Sub



    Private Sub btnMapIt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMapIt.Click

        If Latitude <> String.Empty And Longitude <> String.Empty Then

            Dim f As New frmMap(Latitude, Longitude)
            f.Show()

        End If

    End Sub




End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions