Lesson 2: Finding a Route

Objective: To learn how to find a route between any two points.


1. In Visual Studio, create a new ASP.NET Web Application.


2. Put a Map control on the form by clicking on the Map control in the Toolbox and then dragging and dropping it on the form.
Put a Table control on the form below the Map control (the table will be used to display driving directions).


3. Add references to the AspMap namespace:

Imports AspMap
Imports AspMap.Web


This outlines the general process of finding a route using AspMap:


4. Create a Network object and load the road network using the Open method of the Network object:

Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' create a Network Object
        Dim roadNetwork As AspMap.Network = New AspMap.Network()

        ' load the road network
        roadNetwork.Open(MapPath("MAPS/ROUTING/streets.shp"), MapPath("MAPS/ROUTING/streets.rtn"))

5. Create a Route object.

        ' create a Route Object
        Dim route As AspMap.Route
        route = roadNetwork.CreateRoute()
6. Find the route. The code below finds a quickest route between two locations (specified by latitude/longitude coordinates) using the FindRoute method of the Route object:
        ' start/end coordinates
        Dim StartX, StartY, EndX, EndY As Double
        StartX = -71.5591 ' start longitude
        StartY = 43.1851 ' start latitude
        EndX = -71.5227 ' end longitude
        EndY = 43.2542 ' end latitude

        ' set the route optimization: quickest or shortest
        route.RouteType = RouteType.Quickest

        ' set the current date for turn restrictions
        route.CurrentDate = DateTime.Now

        ' find the route
        If Not route.FindRoute(StartX, StartY, EndX, EndY) Then
                Response.Write("Route not found.")
        End If

7. Add the Route object to the map to display the route:

        ' add the route to the map as a layer
        Dim routeLayer As AspMap.Layer = Map1.AddLayer(route)
        routeLayer.Symbol.LineColor = Color.FromArgb(198, 86, 245)
        routeLayer.Symbol.Size = 4
        ' add the streets.shp layer to the map

        ' set the extent of the route as the current extent of the map
        Map1.Extent = route.Extent      

8. Compile and run the application, your map should look similar to the image below:



Click here to go to Lesson 3.

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