Lesson 3: Generating Driving Directions


Objective: To learn how to generate driving directions for a route.

 

1. To generate driving directions you need to merge the road segments into streets using the MergeSegments method of the Route object:

Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ...

        ' merge the road segments to streets
        route.MergeSegments("NAME;TYPE")
        

2. Add the following GenerateDirections method to generate driving directions. The TurnAngle property of a Street object is used to determine left/right turns (if the turn angle is more than 30 degrees, it is considered a left turn, otherwise if it is less than -30 degrees, it is considered a right turn; a turn angle between 30 and -30 degrees is considered a transition to the next street, without turns):

 

Private Sub GenerateDirections(ByVal route As AspMap.Route)
        Dim direction, distance As String

        Dim streets As Streets = route.Streets

        TR("Directions", "Distance(miles)")

        direction = "Start out going " & streets(0).Direction.ToString() & " on " & streets(0).Name

        If streets.Count > 1 Then
                direction &= " towards " & streets(1).Name
        End If

        distance = Convert.ToString(Math.Round(streets(0).Distance,2))

        TR(direction, distance)

        For i As Integer = 1 To streets.Count - 1
                Dim street As Street = streets(i)

                direction = i & ". "

                If street.TurnAngle > 30 Then
                        direction &= "Turn LEFT onto " & street.Name
                ElseIf street.TurnAngle < -30 Then
                        direction &= "Turn RIGHT onto " & street.Name
                Else
                        direction &= "Road name changes to " & street.Name
                End If

                direction &= " (" & street.Direction.ToString() & ")"

                distance = Convert.ToString(Math.Round(street.Distance,2))

                TR(direction, distance)
        Next i

        direction = "Arrive " & streets(streets.Count - 1).Name

        TR(direction, "")

        direction = "Total Estimated Time: " & Convert.ToString(Math.Round(route.RouteTime/60.0,1)) & " minutes"
        direction &= "Total Distance: " & Convert.ToString(Math.Round(route.RouteDistance,2)) & " miles"
        TR(direction, "")
End Sub

' generates a TR tag
Private Sub TR(ByVal text1 As String, ByVal text2 As String)
        Dim row As TableRow = New TableRow()

        Dim cell As TableCell = New TableCell()
        cell.Text = text1
        row.Cells.Add(cell)

        cell = New TableCell()
        cell.Text = text2
        row.Cells.Add(cell)

        Table1.Rows.Add(row)
End Sub

3. Call the GenerateDirections() method inside the OnLoad event handler:


        ' add driving directions to Table1
        GenerateDirections(route)
End Sub


4. Compile and run the application, your page should look similar to the image below:
 

 

© 2018 VDS Technologies
Converted from CHM to HTML with chm2web Standard 2.85 (unicode)