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 "Traveling Salesman Problem").
Returns:
Route ID string or null.
public static string findRoute( Map map, GeoLocation[] stops, double HWPref, int weight, bool optimize) { RoutingProblem rProblem = map.getRouting().createRoutingProblem(); RoutingStop rStop; for(int i=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); string RouteID = map.getRouting().findRoute(rProblem, false, -1); if( RouteID == "") RouteID = null; return( RouteID); }Example:
<script language="C#" runat="server"> public static void showRoute( Map map, string RouteID) { HttpResponse response = HttpContext.Current.Response; Routing routing = map.getRouting(); routing.setRoute( RouteID); GeoRectangle r = routing.getDrivingDirections( RouteID).getExtent(); map.setExtent( inflateRect(r, 10)); // print driving directions DrivingDirections DDirs = routing.getDrivingDirections( RouteID); DirectionsSegment DSeg; response.Write( "<p>"+DDirs.getStartText()+"<br>\n"); response.Write( DDirs.getFinishText()+"<br>\n"); response.Write( DDirs.getTotalText()+"<p>\n"); for( int i =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 string label; public double x; public double y; public GeoLocation( Map map, string label, double lat, double lon) { Projection proj = map.getProjection(); GeoPoint pt = proj.project( lat, lon); this.label = label; this.x = pt.x; this.y = pt.y; } } public static GeoRectangle inflateRect(GeoRectangle r, double percVal) { double dx = ((r.right - r.left) * percVal) / 100.0; double 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; return (r); } </script> <% Map map = OpenMap(); GeoLocation[] stops = 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); string RouteID = 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()%>">