To find a route, you will need two or more locations represented as the GeoPoint objects, highway preference, and knowledge of what kind of route you'd like to find: shortest, quickest or optimized.
The example code below shows how to find a route using the basic options. For additional options see the RoutingProblem object.
<script language="VBScript" runat=server> Class GeoLocation Public label Public x Public y Sub New( map, ilabel, ilat, ilon) Dim proj, pt proj = map.getProjection() pt = proj.project( ilat, ilon) label = ilabel x = pt.x y = pt.y End Sub End Class Function inflateRect(ByRef r, percVal) Dim dx, dy dx = ((r.right - r.left) * percVal) / 100.0 dy = ((r.top - r.bottom) * percVal) / 100.0 r.left = r.left - dx r.top = r.top + dy r.right = r.right + dx r.bottom = r.bottom - dy inflateRect = r End Function </script> <% Dim map : map = OpenMap() Dim oRouting, DSeg, rProblem, i, rStop, RouteID, r, DDirs ' get the RoutingProblem object rProblem = map.getRouting().createRoutingProblem() ' set routing stops array Dim stops(4) as GeoLocation stops(0) = new GeoLocation( map, "Redlands, CA", 34.0528, -117.1531) stops(1) = new GeoLocation( map, "Loma Linda, CA", 34.0484, -117.2629) stops(2) = new GeoLocation( map, "San Bernardino, CA", 34.1214, -117.3046) stops(3) = new GeoLocation( map, "Highland, CA", 34.1282, -117.2106) ' add routing stops For i = 0 To UBound( stops) - 1 rStop = new RoutingStop rStop.setStop( new GeoPoint( stops(i).x, stops(i).y)) rStop.setLabel( stops(i).label) rProblem.addStop (rStop) Next rProblem.setHighwayPreference( 50) rProblem.setWeight( RoutingProblem.rmShortest) ' to find quickest use rmQuickest rProblem.setOptimize( false) ' no optimization oRouting = map.getRouting() RouteID = oRouting.findRoute(rProblem, true, -1) ' show the route oRouting.setRoute( RouteID) r = oRouting.getDrivingDirections( RouteID).getExtent() map.setExtent( inflateRect(r, 10)) ' increase borders ' print driving directions DDirs = oRouting.getDrivingDirections( RouteID) Response.Write( "<p>" & DDirs.getStartText() & "<br>" & vbCrLf) Response.Write( DDirs.getFinishText() & "<br>" & vbCrLf) Response.Write( DDirs.getTotalText() & "<p>" & vbCrLf) For i = 0 To DDirs.getSegmentCount()-1 DSeg = DDirs.getSegment( i) Response.Write( (i+1) & ". " & DSeg.getText() & "<br>" & vbCrLf) ' if there is TimeLengthText If DSeg.getSegmentType <> DirectionsSegmentType.rmArrive AND _ DSeg.getSegmentType() <> DirectionsSegmentType.rmDepart Then Response.Write( DSeg.getTimeLengthText() & "<br>" & vbCrLf) End If Next %> <img src="<%= map.getMapImageURL(map.getImageWidth(), map.getImageHeight(), ImageFormat.rmDefault)%>" width="<%=map.getImageWidth()%>" height="<%=map.getImageHeight()%>">C# | JScript.NET | Output
Advanced version of the findRoute function VB.NET / C# / JScript.NET.
<% Dim map : map = OpenMap() Dim oRouting, DSeg, rProblem, i, rStop, RouteID, r, DDirs ' get the RoutingProblem object rProblem = map.getRouting().createRoutingProblem() ' set routing stops array Dim stops(2) as GeoLocation stops(0) = new GeoLocation( map, "Redlands, CA", 34.0528, -117.1531) stops(1) = new GeoLocation( map, "Redlands, CO", 39.0854, -108.6515) ' add routing stops For i = 0 To UBound( stops) - 1 rStop = new RoutingStop rStop.setStop( new GeoPoint( stops(i).x, stops(i).y)) rStop.setLabel( stops(i).label) rProblem.addStop (rStop) Next rProblem.setHighwayPreference( 50) rProblem.setWeight( RoutingProblem.rmShortest) ' to find quickest use rmQuickest rProblem.setOptimize( false) ' no optimization ' Trip planning ***************************************************************** ' you should set your local time here to see correct results ' see the Time constructor for details rProblem.setTripStart( new Time( "Thu, 02 Jun 2005 08:00:00 GMT -0600")) ' see TimeSpan rProblem.setDayDriveStart( new com.esri.rmims.TimeSpan( 0, 8, 30, 0)) rProblem.setDayDriveEnd( new com.esri.rmims.TimeSpan( 0, 17, 0, 0)) rProblem.setDriveBetweenRest( new com.esri.rmims.TimeSpan( 0, 3, 0, 0)) rProblem.setRestDuration( new com.esri.rmims.TimeSpan( 0, 0, 30, 0)) rProblem.setDriveFlexibility( new com.esri.rmims.TimeSpan( 0, 0, 30, 0)) rProblem.enableRestBreaks( true) rProblem.enableTripPlanning( true) '******************************************************************************** oRouting = map.getRouting() RouteID = oRouting.findRoute(rProblem, true, -1) ' show the route oRouting.setRoute( RouteID) r = oRouting.getDrivingDirections( RouteID).getExtent() map.setExtent( inflateRect(r, 10)) ' increase borders ' print driving directions DDirs = oRouting.getDrivingDirections( RouteID) Response.Write( "<p>" & DDirs.getStartText() & "<br>" & vbCrLf) Response.Write( DDirs.getFinishText() & "<br>" & vbCrLf) Response.Write( DDirs.getTotalText() & "<p>" & vbCrLf) For i = 0 To DDirs.getSegmentCount()-1 DSeg = DDirs.getSegment( i) Response.Write( (i+1) & ". " & DSeg.getText() & "<br>" & vbCrLf) ' if there is TimeLengthText If DSeg.getSegmentType <> DirectionsSegmentType.rmArrive AND _ DSeg.getSegmentType() <> DirectionsSegmentType.rmDepart Then Response.Write( DSeg.getTimeLengthText() & "<br>" & vbCrLf) End If Next %> <img src="<%= map.getMapImageURL(map.getImageWidth(), map.getImageHeight(), ImageFormat.rmDefault)%>" width="<%=map.getImageWidth()%>" height="<%=map.getImageHeight()%>">C# | JScript.NET | Output