<script language="JScript" runat="server">
	
    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();
    
    // get the RoutingProblem object
    var rProblem : RoutingProblem = map.getRouting().createRoutingProblem();

    // set routing stops array
    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 rStop : RoutingStop;
    // add routing stops
    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);
    }
	
    rProblem.setHighwayPreference( 50);
    rProblem.setWeight(RoutingProblem.rmShortest); // to find quickest use rmQuickest

    rProblem.setOptimize( false); // no optimization

    var routing : Routing = map.getRouting();
    var RouteID : String = routing.findRoute(rProblem, true, -1);

    // 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( 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");
        }
    }	

%>

<img src="<%= map.getMapImageURL(map.getImageWidth(), map.getImageHeight(), ImageFormat.rmDefault)%>"
width="<%=map.getImageWidth()%>" height="<%=map.getImageHeight()%>">