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.
<% Map map = openMap( application); // get the RoutingProblem object RoutingProblem rProblem = map.getRouting().createRoutingProblem(); // set stops 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); // add routing stops for(int i = 0; i < stops.length; i++) { RoutingStop rStop = new RoutingStop(); rStop.setStop(new GeoPoint( stops[i].x, stops[i].y)); rStop.setLabel(stops[i].label); rProblem.addStop(rStop); } rProblem.setHighwayPreference( 50); rProblem.setWeight( RoutingProblem.SHORTEST); // to find quickest use QUICKEST rProblem.setOptimize( false); // no optimization Routing routing = map.getRouting(); String routeID = routing.findRoute(rProblem, true, -1); // show the route routing.setRoute( routeID); GeoRectangle r = routing.getDrivingDirections( routeID).getExtent(); map.setExtent( inflateRect(r, 10)); //increase borders // print driving directions DrivingDirections dDirs = routing.getDrivingDirections( routeID); DirectionsSegment dSeg; out.println( "<p>"+dDirs.getStartText()+"<br>"); out.println( dDirs.getFinishText()+"<br>"); out.println( dDirs.getTotalText()+"<p>"); for( int i=0; i < dDirs.getSegmentCount(); i++) { dSeg = dDirs.getSegment( i); out.println( (i+1)+". " + dSeg.getText()+"<br>"); // if there is TimeLengthText if( dSeg.getSegmentType() != DirectionsSegment.ARRIVE && dSeg.getSegmentType() != DirectionsSegment.DEPART) out.println( dSeg.getTimeLengthText()+"<br>"); } %> <% public GeoRectangle inflateRect(GeoRectangle r, int percVal) { double dx = ((r.right - r.left) * percVal) / 100.0; double dy = ((r.top - r.bottom) * percVal) / 100.0; r.left -= dx; r.top += dy; r.right += dx; r.bottom -= dy; return r; } public class GeoLocation { public String label; public double x; public double y; GeoLocation (Map map, String iLabel, double iLat, double iLon) throws RMException { Projection proj = map.getProjection(); GeoPoint pt = proj.project( iLat, iLon); label = iLabel; x = pt.x; y = pt.y; } } %> <img src="<%= map.getMapImageURL(map.getImageWidth(), map.getImageHeight(), ImageFormat.DEFAULT)%>" width="<%=map.getImageWidth()%>" height="<%=map.getImageHeight()%>">Output
Advanced version of the findRoute function.
<% Map map = openMap( application); // get the RoutingProblem object RoutingProblem rProblem = map.getRouting().createRoutingProblem(); // set routing stops array GeoLocation[] stops = new GeoLocation[2]; stops[0] = new GeoLocation (map, "Redlands, CA", 34.0528, -117.1531); stops[1] = new GeoLocation (map, "Redlands, CO", 39.0854, -108.6515); RoutingStop rStop; // add routing stops 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); } rProblem.setHighwayPreference( 50); rProblem.setWeight(RoutingProblem.SHORTEST); // to find quickest use QUICKEST rProblem.setOptimize( false); // no optimization // Trip planning ***************************************************************** // you should set your local time here to see correct results rProblem.setTripStart( new Time( "Thu, 02 Jun 2005 08:00:00 GMT -0600")); rProblem.setDayDriveStart( new TimeSpan( 0, 8, 30, 0)); rProblem.setDayDriveEnd( new TimeSpan( 0, 17, 0, 0)); rProblem.setDriveBetweenRest( new TimeSpan( 0, 3, 0, 0)); rProblem.setRestDuration( new TimeSpan( 0, 0, 30, 0)); rProblem.setDriveFlexibility( new TimeSpan( 0, 0, 30, 0)); rProblem.enableRestBreaks( true); rProblem.enableTripPlanning( true); //******************************************************************************** Routing routing = map.getRouting(); String RouteID = routing.findRoute(rProblem, true, -1); // show the route routing.setRoute( RouteID); GeoRectangle r = routing.getDrivingDirections( RouteID).getExtent(); map.setExtent( inflateRect(r, 10)); // increase borders // print driving directions DrivingDirections dDirs = routing.getDrivingDirections( RouteID); DirectionsSegment dSeg; out.println( "<p>"+dDirs.getStartText()+"<br>"); out.println( dDirs.getFinishText()+"<br>"); out.println( dDirs.getTotalText()+"<p>"); for( int i = 0; i < dDirs.getSegmentCount(); i++) { dSeg = dDirs.getSegment( i); out.println( (i+1)+". " + dSeg.getText()+"<br>"); // if there is TimeLengthText if( dSeg.getSegmentType() != DirectionsSegment.ARRIVE && dSeg.getSegmentType() != DirectionsSegment.DEPART) out.println( dSeg.getTimeLengthText()+"<br>"); } %> <img src="<%= map.getMapImageURL(map.getImageWidth(), map.getImageHeight(), ImageFormat.DEFAULT)%>" width="<%=map.getImageWidth()%>" height="<%=map.getImageHeight()%>">