Routes in VB.NET 2010

Topics: General
May 28, 2010 at 9:23 PM
Edited May 28, 2010 at 9:24 PM
Hey,

Two things really. Firstly, is there a demo project for vb.net? I am not farmiliar with C++ or C#, and cannot seem to find one!

The second issue is that I cannot get routes working with vb.net. The following is my code. Basically, the user has a predefined adress, and enters another address. Then it calculates the route between the two.

Dim g1 As GMap.NET.PointLatLng
g1 = Me.CurrentMarker.Position
map.CurrentPosition = g1


Dim code As GMap.NET.GeoCoderStatusCode = map.SetCurrentPositionByKeywords(variables.address1.Text & ", " & variables.city.Text & ", " & variables.postcode.Text & ",United Kingdom")
'Gestione Current Marker
Me.CurrentMarker = New GMap.NET.WindowsForms.Markers.GMapMarkerGoogleRed( _
Me.map.CurrentPosition)

'Me.currentMarker.TooltipMode = GMap.NET.WindowsForms.MarkerTooltipMode.Always
Me.CurrentMarker.ToolTipText = variables.clubname.Text
Me.TopOverlay.Markers.Add(Me.CurrentMarker)

Dim g2 As GMap.NET.PointLatLng = Me.CurrentMarker.Position
'Dim route As MapRoute
Dim route As GMap.NET.MapRoute
Dim r As GMap.NET.WindowsForms.GMapRoute
Dim routes As GMap.NET.WindowsForms.GMapOverlay
route.Points.Add(g1)
route.Points.Add(g2)

r.Points.AddRange(route.Points)

Me.TopOverlay.Routes.Add(r)
May 28, 2010 at 10:10 PM

is ir really so hard to view demo in reflector and change language to vb? ;}

 

Private Sub button3_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim route As MapRoute = Singleton(Of GMaps).Instance.GetRouteBetweenPoints(Me.start, Me.end, False, CInt(Me.MainMap.Zoom))
    If (Not route Is Nothing) Then
        Dim item As New GMapRoute(route.Points, route.Name)
        Me.routes.Routes.Add(item)
        Dim marker As GMapMarker = New GMapMarkerGoogleRed(Me.start) { _
            .ToolTipText = ("Start: " & route.Name), _
            .ToolTipMode = MarkerTooltipMode.Always _
        }
        Dim marker2 As GMapMarker = New GMapMarkerGoogleGreen(Me.end) { _
            .ToolTipText = ("End: " & Me.end.ToString), _
            .ToolTipMode = MarkerTooltipMode.Always _
        }
        Me.objects.Markers.Add(marker)
        Me.objects.Markers.Add(marker2)
        Me.MainMap.ZoomAndCenterRoute(item)
    End If
End Sub


 

May 28, 2010 at 10:20 PM
Hey, Thanks for the reply! I think I have some issue in declaring the actual variables. As in all the exmaples I have found, the code says " Dim x as MapRoute" However I always have to add the "Global.Gmap.Net." or "Global.Gmap.Net.WindowsForms" beforehand. So in the example you have given me there, Singleton(Of GMaps) the singleton isn't declared and neither is the GMaps. And I cna't find any way to append them with the "Global.GMap.Net" string. Thanks for your reply though! I love the software
May 28, 2010 at 10:32 PM

i;ve just copied translation, i can't guarantee it's good, i'm not really vb expert ;}

May 29, 2010 at 7:48 AM
adamlonsdale wrote:
Hey,

Two things really. Firstly, is there a demo project for vb.net? I am not farmiliar with C++ or C#, and cannot seem to find one!

The second issue is that I cannot get routes working with vb.net. The following is my code. Basically, the user has a predefined adress, and enters another address. Then it calculates the route between the two.

Dim g1 As GMap.NET.PointLatLng
g1 = Me.CurrentMarker.Position
map.CurrentPosition = g1


Dim code As GMap.NET.GeoCoderStatusCode = map.SetCurrentPositionByKeywords(variables.address1.Text & ", " & variables.city.Text & ", " & variables.postcode.Text & ",United Kingdom")
'Gestione Current Marker
Me.CurrentMarker = New GMap.NET.WindowsForms.Markers.GMapMarkerGoogleRed( _
Me.map.CurrentPosition)

'Me.currentMarker.TooltipMode = GMap.NET.WindowsForms.MarkerTooltipMode.Always
Me.CurrentMarker.ToolTipText = variables.clubname.Text
Me.TopOverlay.Markers.Add(Me.CurrentMarker)

Dim g2 As GMap.NET.PointLatLng = Me.CurrentMarker.Position
'Dim route As MapRoute
Dim route As GMap.NET.MapRoute
Dim r As GMap.NET.WindowsForms.GMapRoute
Dim routes As GMap.NET.WindowsForms.GMapOverlay
route.Points.Add(g1)
route.Points.Add(g2)

r.Points.AddRange(route.Points)

Me.TopOverlay.Routes.Add(r)

Hi.As you can see here http://greatmaps.codeplex.com/Thread/View.aspx?ThreadId=214099 i have the same problem with routes in vb.The routes works fine in a small c# project i have made but in vb i cannot find the way to calculate route.If you find any solution i appreciate if you share it with me.Thanks
May 29, 2010 at 3:02 PM

Teach Yourself Programming in Ten Years ;}

Jun 18, 2011 at 1:50 PM

Hi Radioman,

Now I read all about Routes between 2 points in the discussion and it seem I have the same problem as other VB 2010 user.

I opend your Demo in C# and translated all code that I think " GetRouteBetweenPoints " needs from C# to VB

In your Demo the routes work perfect from the C# code on the same PC as I try the VB.Net code.

I am using VB.net Express and C# Express

When I run the line:

Dim route As MapRoute = GMaps.Instance.GetRouteBetweenPoints(r_start, r_end, False, CInt(GMapControl1.Zoom))

route always retuns nothing. r_start and r_end gets xy coordinates from 2 points by click on the map like in your Demo.

r_start = {Lat=-23,5111078191942, Lng=-47,4746704101563}

r_end = {Lat=-23,3372121665012, Lng=-47,8482055664063}

False = False

CInt(GMapControl1.Zoom) = 9

Returns:

route = Nothing

Debuging step by step I get a message in the direct window but no error.

Einzelschritt: Nichtbenutzercode "GMap.NET.Singleton<GMap.NET.GMaps>.Instance.get" wird übersprungen.
Einzelschritt: Nichtbenutzercode "WindowsApplication1.Form1.GMapControl1.get" wird übersprungen.

Something like: Step: Nonusercode "......" Skipped

Here my code, please have a look if you find out what I an doing wrong.

Thank you

Thomas

Declaration
Imports GMap.NET.WindowsForms 
Imports
GMap.NET.WindowsForms.Markers
Imports
GMap.NET.WindowsForms.ToolTips
Form1
Public Class Form1
    Dim objects As GMapOverlay
    Dim routes As GMapOverlay
    Dim topo As GMapOverlay
    Dim r_start As PointLatLng
    Dim r_end As PointLatLng
    Dim currentMarker As GMapMarker
    'Dim CurentRectMarker As GMapMarkerRect = Nothing
    Dim center As GMapMarker
    Dim isMouseDown As Boolean = False

Load
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.ComboBoxMapType.DataSource = [Enum].GetValues(GetType(MapType))
        ComboBoxMapType.SelectedIndex = 11
        routes = New GMapOverlay(GMapControl1, "routes")
        GMapControl1.Overlays.Add(routes)
        objects = New GMapOverlay(GMapControl1, "objects")
        GMapControl1.Overlays.Add(objects)
        topo = New GMapOverlay(GMapControl1, "topo")
        GMapControl1.Overlays.Add(topo)
        
      
        ' set current marker
        currentMarker = New GMapMarkerGoogleRed(GMapControl1.Position)
        currentMarker.IsHitTestVisible = False
        topo.Markers.Add(currentMarker)

        ' map center
        center = New GMapMarkerCross(GMapControl1.Position)
        topo.Markers.Add(center)
    End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


        Dim route As MapRoute = GMaps.Instance.GetRouteBetweenPoints(r_start, r_end, False, CInt(GMapControl1.Zoom))

        If route IsNot Nothing Then
            ' add route
            Dim r As New GMapRoute(route.Points, route.Name)
            routes.Routes.Add(r)

            ' add route start/end marks
            Dim m1 As GMapMarker = New GMapMarkerGoogleRed(r_start)
            m1.ToolTipText = "Start: " + route.Name
            m1.ToolTipMode = MarkerTooltipMode.Always

            Dim m2 As GMapMarker = New GMapMarkerGoogleGreen(r_end)
            m2.ToolTipText = "End: " & r_end.ToString()
            m2.ToolTipMode = MarkerTooltipMode.Always

            objects.Markers.Add(m1)
            objects.Markers.Add(m2)

            GMapControl1.ZoomAndCenterRoute(r)
        End If
    End Sub



    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        r_start = currentMarker.Position
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        r_end = currentMarker.Position
    End Sub

    Private Sub GMapControl1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles GMapControl1.MouseUp
        If e.Button = MouseButtons.Left Then
            isMouseDown = False
        End If
    End Sub
Private Sub GMapControl1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles GMapControl1.MouseDown
        If e.Button = MouseButtons.Left Then
            isMouseDown = True

            If currentMarker.IsVisible Then
                currentMarker.Position = GMapControl1.FromLocalToLatLng(e.X, e.Y)

                Dim px = GMapControl1.Projection.FromLatLngToPixel(currentMarker.Position.Lat, currentMarker.Position.Lng, CInt(GMapControl1.Zoom))
                Dim tile = GMapControl1.Projection.FromPixelToTileXY(px)

                
End If End If End Sub
Private Sub GMapControl1_OnCurrentPositionChanged1(ByVal point As GMap.NET.PointLatLng) Handles GMapControl1.OnCurrentPositionChanged center.Position = point End Sub
Private Sub GMapControl1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles GMapControl1.MouseEnter
        GMapControl1.Focus()
    End Sub

    Private Sub GMapControl1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles GMapControl1.MouseMove

        Dim statusOSM As Object
        Try
            statusOSM = GMapControl1.FromLocalToLatLng(e.X, e.Y)

            ToolStripStatusLabel1.Text = statusOSM.ToString
        Catch ex As Exception

        End Try
    End Sub

Jun 18, 2011 at 2:26 PM

tried debug?

Jun 18, 2011 at 2:28 PM

Dim route As MapRoute = Singleton(Of GMaps).Instance.GetRouteBetweenPoints(Me.start, Me.end, False, CInt(Me.MainMap.Zoom))
If (Not route Is Nothing) Then

Jun 18, 2011 at 2:42 PM

Hi Radioman,

I changed my code to

Dim route As MapRoute = Singleton(Of GMaps).Instance.GetRouteBetweenPoints(r_start, r_end, False, CInt(GMapControl1.Zoom))

If (Not route Is Nothing) Then

The result is the same :-( 

route = Nothing

What can be different between C# and VB.net in this call?

Jun 18, 2011 at 2:52 PM

maybe in that location google routing isn't available, try other locations

Jun 18, 2011 at 3:02 PM

I changed to Germany set start and end on the Highways, the same result, I tried google, OSM, Bing...

route = Nothing

Your Demo works routing small roads in the jungle of Brazil on OSM, and Google  :-)

Jun 18, 2011 at 3:07 PM

can you do step by step in debug mode?

Jun 18, 2011 at 3:34 PM

Here some screenshots step by step

http://www.ambiente.com.br/zero-race/index.php?level=album&id=6

In the direct window this messages.

Einzelschritt: Nichtbenutzercode "GMap.NET.Singleton<GMap.NET.GMaps>.Instance.get" wird übersprungen.
Einzelschritt: Nichtbenutzercode "WindowsApplication1.Form1.GMapControl1.get" wird übersprungen.

Something like: SingleStep: Nonusercode "......" Skipped

Jun 18, 2011 at 3:50 PM

i mean deeper into this function...

Jun 18, 2011 at 11:42 PM

Hi Radioman,

I got the error, I forgot to add the System.Data.SQLite.DLL to my references.

Now it works perfect like in your Demo.

Thank you for your assistance.

Thomas