Section 7.3. Displaying Details of a Route


7.3. Displaying Details of a Route

A Route object contains several details about a calculated route, including the total driving time, trip time, distance traveled, and detailed segment level driving directions, distance, and time.

7.3.1. Displaying the Route Summary

Route summary information contains the details, such as the amount of time the entire trip took, total time spent driving, and total distance traveled. This information can be obtained from the RouteItinerary object instance. A valid route exposes the RouteItinerary object via the Itinerary field, and the RouteItinerary object provides several fields, such as Distance, tripTime, and DrivingTime, to provide information about total distance traveled, total trip time, and time spent driving, respectively. The following code shows how to access this information from a Route object:

     //Getting the Route Summary     //Get total distance     string distance = route.Itinerary.Distance.ToString("#.##");     //Get total drive time     string totalDriveTime = String.Format("Total drive time: {0} Hours",                 (Convert.ToDouble(route.Itinerary.DrivingTime)/                                     (60 * 60)).ToString("#,##"));     //Get total trip time     string totalTripTime = String.Format("Total trip time: {0} Hours",                 (Convert.ToDouble(route.Itinerary.TripTime)/                                     (60 * 60)).ToString("#.##"));

The distance is expressed in kilometers by default unless you set it otherwise as shown in the previous section. The trip and drive times are expressed in seconds, and in this example, I convert them into hours.

A route summary with the previous example's formatting looks as follows:

     Total Distance: 198.42 (Miles)     Total drive time: 3.94 Hours     Total trip time: 3.94 Hours

You might notice that the trip and drive times are the same in this case; however, if you set the driving day start and end timings and your route spans across multiple days, you see a difference between the trip time and the drive time (since trip time is the amount of time spent both driving and resting).

7.3.2. Displaying Route Details

Route details include the segment-level driving instructions along with the bearing and distance. These can be obtained from the Segment array in the RouteItinerary.Segments field.

Each Segment object contains a collection of Direction objects, distance traveled, driving time, trip time, and a LatLong object. Each Direction object contains information such as driving directions, driving instructions, and bearing directions that are used to display detailed driving directions.

The following example shows how to use the RouteItinerary.Segments to display detailed driving directions:

     //Get the directions     foreach(Segment segment in route.Itinerary.Segments)     {         //Get segment distance         if(segment.Distance > 0)         {             string segmentDistance = segment.Distance.ToString("#.##");            //Display segment distance            Console.WriteLine(segmentDistance.ToString());         }         //Get directions for each segment         foreach(Direction direction in segment.Directions)         {             //Simple use             //string instruction = direction.Instruction;             //Complex use             //See whether we need this direction as a specific entry             switch(direction.Action)             {                 case DirectionAction.Depart:                 case DirectionAction.Arrive:                     //Display instruction                     //Ex: Arrive Finish                     Console.WriteLine(direction.Instruction);                     break;                 case DirectionAction.Other:                     //State Borders etc                     //Display only as needed                     //Ex: Entering Oregon                     Console.WriteLine(direction.Instruction);                     break;                 default:                     if(direction.Towards != null &&                         direction.Towards != string.Empty)                     {                        //Display instruction along with "towards" text                     //Ex: Take Ramp (LEFT) onto I-405 [I-405 / Renton]                        Console.WriteLine(                        String.Format("\t\t {0} [{1}]\r\n",                                      direction.Instruction,                                      direction.Towards));                        */                     }                     else                     {                        //Display instruction along with no "towards" text                        //Ex: Take Ramp (LEFT) onto I-405                        Console.WriteLine(                        String.Format("\t\t {0} \r\n",                                      direction.Instruction));                     }                     break;             }         }     }

As you can see, each Direction instance contains a DirectionAction enumeration that you can use to identify what kind of information the current instance contains. In this example, I have considered only a couple of actions such as Arrive, Depart, and Other, but you can add more custom driving directions display logic around the rest of the direction action enumerations.




Programming MapPoint in  .NET
Programming MapPoint in .NET
ISBN: 0596009062
EAN: 2147483647
Year: 2005
Pages: 136
Authors: Chandu Thota

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net