Parameters:
map - Map object;
stops - array of locations;
HWPref - highway preference (0-100);
weight - route preference (quickest or shortest);
optimize - enables or disables the optimizing route parameter (also known as the "Travelling Salesman Problem").
Returns:
Route ID string or null.
public function findRoute( map : Map, stops : GeoLocation[], HWPref : double, weight : int, optimize : boolean) : String { var rProblem : RoutingProblem = map.getRouting().createRoutingProblem(); var rStop : RoutingStop ; for(var i : int =0; i < stops.length; i++) { rStop = new RoutingStop(); rStop.setStop(new GeoPoint( stops[i].x, stops[i].y)); rStop.setLabel(stops[i].label); rProblem.addStop(rStop); } if ( weight == RoutingProblem.rmQuickest ) { rProblem.setHighwayPreference(HWPref); rProblem.setWeight(RoutingProblem.rmQuickest); // find quickest route } else { rProblem.setWeight(RoutingProblem.rmShortest); // find shortest route } rProblem.setOptimize( optimize); var RouteID : String = map.getRouting().findRoute(rProblem, false, -1); if( RouteID == "") RouteID = null; return( RouteID); }Example:
<script language="JScript" runat="server"> public function showRoute( map : Map, RouteID : String) { var routing : Routing = map.getRouting(); // show the route routing.setRoute( RouteID); var r : GeoRectangle = routing.getDrivingDirections( RouteID).getExtent(); map.setExtent( inflateRect(r, 10)); // increase borders // print driving directions var DDirs : DrivingDirections = routing.getDrivingDirections( RouteID); var DSeg : DirectionsSegment; Response.Write( "<p>"+DDirs.getStartText()+"<br>\n"); Response.Write( DDirs.getFinishText()+"<br>\n"); Response.Write( DDirs.getTotalText()+"<p>\n"); for( var i : int = 0; i < DDirs.getSegmentCount(); i++) { DSeg = DDirs.getSegment( i); Response.Write( (i+1)+". " + DSeg.getText() + "<br>\n"); // if there is TimeLengthText if(DSeg.getSegmentType() != DirectionsSegmentType.rmArrive && DSeg.getSegmentType() != DirectionsSegmentType.rmDepart) { Response.Write( DSeg.getTimeLengthText() + "<br>\n"); } } } public class GeoLocation { public var label : String; public var x : double; public var y : double; public function GeoLocation( map : Map, label : String, lat : double, lon : double) { var proj : Projection = map.getProjection(); var pt : GeoPoint = proj.project( lat, lon); this.label = label; this.x = pt.x; this.y = pt.y; } } public function inflateRect(r : GeoRectangle, percVal : double) : GeoRectangle { var dx : double = ((r.right - r.left) * percVal) / 100.0; var dy : double = ((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; return (r); } </script> <% var map : Map = OpenMap(); var stops : GeoLocation[] = new GeoLocation[4]; 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); var RouteID : String = findRoute( map, stops, 50, RoutingProblem.rmShortest, false); if( RouteID != null) showRoute(map, RouteID); %> <img src="<%= map.getMapImageURL(map.getImageWidth(), map.getImageHeight(), ImageFormat.rmDefault)%>" width="<%=map.getImageWidth()%>" height="<%=map.getImageHeight()%>">