VB 2008 vehicle siimulation, marker redraw on map

Jun 26, 2009 at 6:37 PM

Hi

 

It is possible to make a simulation in vb 2008, like changing the current marker position along the route?

 

I want to simulate the vehicle from start point to end point using the pointlatlng collection, and after that, using a list created in real time from data received from a gps, i want to view in real time the vehicle on map...

 

I accept all kind of suggestions and corrections to my code.

 

Once again, congratulations for the great job.

 

This is my code so far,

Imports System.Globalization
Imports GMap.NET
Imports GMap.NET.WindowsForms
Imports GMap.NET.WindowsForms.Markers
Imports System.Windows.Forms.PaintEventArgs





'-----------------------------------------------------------------------------------------------------------------'
'
'-----------------------------------------------------------------------------------------------------------------'
Public Class DistCalc
    '-----------------------------------------------------------------------------------------------------------------'
    '      Global vars
    '-----------------------------------------------------------------------------------------------------------------'
    Dim status As GeoCoderStatusCode
    Dim ResultadoPoints As MapRoute
    Dim sPoint As New PointLatLng
    Dim ePoint As New PointLatLng
    Dim stat As Boolean = False
    Dim zoomControl As MapRoute




    '-----------------------------------------------------------------------------------------------------------------'
    '       Get distance/ add route from latitude and longitude
    '-----------------------------------------------------------------------------------------------------------------'
    Private Sub distFormcoord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles distFormcoord.Click

        If useLatlng.Checked = True Then

            clear()
            status = GeoCoderStatusCode.Unknow

            stat = True

            ResultadoPoints = GMaps.Instance.GetRouteBetweenPoints(p1.Text, p2.Text, avoidHigh.Checked, mainMap.Zoom)

            Dim limit As Integer = ResultadoPoints.Points.Count
            Dim point_1 As PointLatLng = ResultadoPoints.Points(0)
            Dim point_2 As PointLatLng = ResultadoPoints.Points(limit - 1)
            Dim routes As New GMapRoute(ResultadoPoints.Points, ResultadoPoints.Name)
            Dim objects As New GMapOverlay(mainMap, "stops")
            Dim stPoint As GMapMarker = New Markers.GMapMarkerGoogleGreen(point_1)
            Dim enPoint As GMapMarker = New Markers.GMapMarkerGoogleRed(point_2)

            '-----------------------------------------------------------------------------------------------------------------'
            '        Road distance
            '-----------------------------------------------------------------------------------------------------------------'

            distTotlatlng.Text = ResultadoPoints.Distance.ToString("0.000") & "  " & "Km"

            '-----------------------------------------------------------------------------------------------------------------'
            '        Add markers/routes
            '-----------------------------------------------------------------------------------------------------------------'

            mainMap.ReloadMap()

            stPoint.ToolTipText = "SP"
            enPoint.ToolTipText = "EP"
           
            stPoint.TooltipMode = MarkerTooltipMode.Always
            enPoint.TooltipMode = MarkerTooltipMode.Always

            objects.Routes.Add(routes)
            objects.Markers.Add(stPoint)
            objects.Markers.Add(enPoint)

            If avoidHigh.Checked Then

                routes.Color = Color.Black

            End If

            mainMap.Overlays.Add(objects)
            mainMap.ZoomAndCenterRoute(routes)

            sPoint = point_1
            ePoint = point_2
            zoomControl = routes

        End If

    End Sub
    '-----------------------------------------------------------------------------------------------------------------'
    '       Get distance by names
    '-----------------------------------------------------------------------------------------------------------------'
    Private Sub getDist_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles getDist.Click

        mainMap.ReloadMap()


        If spAdr.Text & spCty.Text & spZcd.Text & spCry.Text <> "" Then

            status = GeoCoderStatusCode.Unknow

            stat = True

            Dim point_1 As PointLatLng = GMaps.Instance.GetLatLngFromGeocoder _
            ((spAdr.Text & "," & spCty.Text & "," & spZcd.Text & "," & spCry.Text), status)
            Dim point_2 As PointLatLng = GMaps.Instance.GetLatLngFromGeocoder _
            ((epAdr.Text & "," & epCty.Text & "," & epZcd.Text & "," & epCry.Text), status)

            ResultadoPoints = GMaps.Instance.GetRouteBetweenPoints(point_1, point_2, avoidHigh.Checked, mainMap.Zoom)

            Dim routes As New GMapRoute(ResultadoPoints.Points, ResultadoPoints.Name)
            Dim objects As New GMapOverlay(mainMap, "stops")
            
            Dim stPoint As GMapMarker = New Markers.GMapMarkerGoogleGreen(point_1)
            Dim enPoint As GMapMarker = New Markers.GMapMarkerGoogleRed(point_2)
            '-----------------------------------------------------------------------------------------------------------------'
            '       LatLng display
            '-----------------------------------------------------------------------------------------------------------------'
            coordinatesDisplay(point_1, point_2)
            '-----------------------------------------------------------------------------------------------------------------'
            '       Road distance
            '-----------------------------------------------------------------------------------------------------------------'
            distTot.Text = ResultadoPoints.Distance.ToString("0.000") & "  " & "Km"

            '-----------------------------------------------------------------------------------------------------------------'
            '       Add markers/routes
            '-----------------------------------------------------------------------------------------------------------------'
            mainMap.ReloadMap()

            stPoint.ToolTipText = "SP"
            enPoint.ToolTipText = "EP"

            stPoint.TooltipMode = MarkerTooltipMode.Always
            enPoint.TooltipMode = MarkerTooltipMode.Always

            objects.Routes.Add(routes)
            objects.Markers.Add(stPoint)
            objects.Markers.Add(enPoint)

           
            If avoidHigh.Checked Then

                routes.Color = Color.Green

            End If

            mainMap.Overlays.Add(objects)
            mainMap.ZoomAndCenterRoute(routes)

            sPoint = point_1
            ePoint = point_2

            zoomControl = routes
            TextBox1.Text = GMap.NET.GMaps.Instance.GetDistance(sPoint, ePoint).ToString("0.000") & "  " & "Km"
        End If

    End Sub
    '-----------------------------------------------------------------------------------------------------------------'
    '       Map type
    '-----------------------------------------------------------------------------------------------------------------'


    Private Sub mainmaptype_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles mainmaptype.TextChanged

        mainMap.MapType = mainmaptype.SelectedItem
        mainMap.ReloadMap()

    End Sub
    '-----------------------------------------------------------------------------------------------------------------'
    '       Map reload current position set by name
    '-----------------------------------------------------------------------------------------------------------------'

    Private Function mapReload(ByVal quickPos As String)

        mainMap.Overlays.Clear()

        With mainMap

            .SetCurrentPositionByKeywords(quickPos)

            Dim myMarker As New GMapMarkerCross(mainMap.CurrentPosition)
            Dim objects As New GMapOverlay(mainMap, "stops")

            .MapType = mainmaptype.SelectedItem
            .MaxZoom = 16
            .MinZoom = 2
            .Zoom = 14
            .ReloadMap()

            objects.Markers.Add(myMarker)
            mainMap.Overlays.Add(objects)

        End With

        Return 1

    End Function
    '-----------------------------------------------------------------------------------------------------------------'
    '       Map reload 2 current position as pointlang geocode
    '-----------------------------------------------------------------------------------------------------------------'

    Private Function mapReload_2(ByVal quickPos As PointLatLng)


        With mainMap

            .CurrentPosition = quickPos

            Dim myMarker As New GMapMarkerCross(mainMap.CurrentPosition)
            Dim objects As New GMapOverlay(mainMap, "stops")

            .MapType = mainmaptype.SelectedItem
            .MaxZoom = 16
            .MinZoom = 2
            .Zoom = 12
            '.ZoomAndCenterRoute(zoomControl)
            .ReloadMap()

            objects.Markers.Add(myMarker)
            mainMap.Overlays.Add(objects)

        End With

        Return 1


    End Function
    '-----------------------------------------------------------------------------------------------------------------'
    '       Add pins on route seg
    '-----------------------------------------------------------------------------------------------------------------'
    Private Sub addSegPins_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addSegPins.Click


        If stat = True Then


            Dim tPontos As Integer = ResultadoPoints.Points.Count


            pointsTest.Text = ResultadoPoints.Points.Count.ToString

            For ind = 0 To tPontos - 1 'Step 20

                Dim objects As New GMapOverlay(mainMap, "stops")
                Dim mMarker As GMapMarker = New Markers.GMapMarkerGoogleRed(ResultadoPoints.Points(ind))

                'mMarker.ToolTipText = ResultadoPoints.Points(ind).ToString
                mMarker.TooltipMode = MarkerTooltipMode.Always
                'mMarker.Position = ResultadoPoints.Points(ind)

                objects.Markers.Add(mMarker)
                mainMap.Overlays.Add(objects)

            Next

        End If

        stat = False

    End Sub
    '-----------------------------------------------------------------------------------------------------------------'
    '       Go to start point
    '-----------------------------------------------------------------------------------------------------------------'
    Private Sub start_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles start.Click

        Dim checkBox As Boolean

        checkBox = useLatlng.Enabled

        Select Case checkBox

            Case True

                Dim spoint_1 As PointLatLng = sPoint
                mapReload_2(spoint_1)

            Case False

                Dim point_1 As PointLatLng = GMaps.Instance.GetLatLngFromGeocoder _
                ((spAdr.Text & "," & spCty.Text & "," & spZcd.Text & "," & spCry.Text), status)

                mapReload_2(point_1)

        End Select

    End Sub
    '-----------------------------------------------------------------------------------------------------------------'
    '       Go to end point
    '-----------------------------------------------------------------------------------------------------------------'

    Private Sub end_1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles end_1.Click

        Dim checkBox As Boolean

        checkBox = useLatlng.Enabled

        Select Case checkBox

            Case True

                Dim epoint_2 As PointLatLng = ePoint

                mapReload_2(epoint_2)

            Case False

                Dim point_2 As PointLatLng = GMaps.Instance.GetLatLngFromGeocoder _
                ((epAdr.Text & "," & epCty.Text & "," & epZcd.Text & "," & epCry.Text), status)

                mapReload_2(point_2)

        End Select

    End Sub
    
    '-----------------------------------------------------------------------------------------------------------------'
    '       Clear fields button
    '-----------------------------------------------------------------------------------------------------------------'
    Private Sub cls_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cls.Click

        clear()
        mapReload("Setubal")

    End Sub
    '-----------------------------------------------------------------------------------------------------------------'
    '       Clear fields function
    '-----------------------------------------------------------------------------------------------------------------'
    Private Function clear()

        Dim controle As Control
        Dim i As Integer

        For i = 0 To Panel3.Controls.Count - 1
            controle = Panel3.Controls(i)

            If TypeOf controle Is TextBox Then
                controle.Text = ""

            End If

        Next i

        Return 1

    End Function
    '-----------------------------------------------------------------------------------------------------------------'
    '       Close aplication
    '-----------------------------------------------------------------------------------------------------------------'
    Private Sub closeAp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles closeAp.Click, closeAp.Click

        Me.Close()

    End Sub
    '-----------------------------------------------------------------------------------------------------------------'
    '       Quick search
    '-----------------------------------------------------------------------------------------------------------------'
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim point_1 As String = spCty.Text

        mapReload(point_1)

    End Sub
    '-----------------------------------------------------------------------------------------------------------------'
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Dim point_2 As String = epCty.Text

        mapReload(point_2)

    End Sub
    '-----------------------------------------------------------------------------------------------------------------'
    '       Arranque
    '-----------------------------------------------------------------------------------------------------------------'
    Private Sub DistCalc_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.WindowState = FormWindowState.Maximized

        AddHandler mainMap.MouseDoubleClick, AddressOf mainMap_hidepanel3

        mainmaptype.DataSource = [Enum].GetValues(GetType(MapType))
        mainmaptype.SelectedItem = mainMap.MapType

        With mainMap

            .SetCurrentPositionByKeywords("setubal")

            Dim myMarker As New GMapMarkerCross(mainMap.CurrentPosition)
            Dim objects As New GMapOverlay(mainMap, "stops")

            .MapType = mainmaptype.SelectedItem
            .MaxZoom = 16
            .MinZoom = 2
            .Zoom = 14
            .ReloadMap()

            objects.Markers.Add(myMarker)
            mainMap.Overlays.Add(objects)

        End With

        UpdateCurrentMarkerPositionText()

    End Sub
    '-----------------------------------------------------------------------------------------------------------------'
    '       Update current position text
    '-----------------------------------------------------------------------------------------------------------------'     
    Private Sub UpdateCurrentMarkerPositionText()

        textBoxLat.Text = mainMap.CurrentPosition.Lat.ToString(CultureInfo.InvariantCulture)
        textBoxLng.Text = mainMap.CurrentPosition.Lng.ToString(CultureInfo.InvariantCulture)

    End Sub
    '-----------------------------------------------------------------------------------------------------------------'
    '       LatLng display
    '-----------------------------------------------------------------------------------------------------------------'
    Private Sub coordinatesDisplay(ByVal _start, ByVal _end)
        p1.Text = Replace(_start.Lat.ToString, ",", ".") & "," & Replace(_start.Lng.ToString, ",", ".")
        p2.Text = Replace(_end.Lat.ToString, ",", ".") & "," & Replace(_end.Lng.ToString, ",", ".")
    End Sub
    '-----------------------------------------------------------------------------------------------------------------'
    '       Close panel 3
    '-----------------------------------------------------------------------------------------------------------------'
    Private Controlo As Boolean = True
    Private Sub mainMap_hidepanel3(ByVal sender As Object, ByVal e As MouseEventArgs)


        If Controlo = True And e.Button = MouseButtons.Left Then
            Panel3.Hide()
            Controlo = False
        Else
            Panel3.Show()
            Controlo = True
        End If
    End Sub

   

    

End Class
<font size="3"><font face="Times New Roman">

 

</font></font>
<font size="3">

 

</font>

 

 

Jun 26, 2009 at 6:54 PM

..well isn't it just as simple as just updatting positions of vehicles using timer?