Multi Route CalculateRoutes (turn-by-turn directions & maps)

 

Please see CalculateRoute method first before continuing.  This is multiple route version of the CalculateRoute method.  It returns map of each individual leg of the each route, route map, and then a multi route map of all the routes involved. Like CalculateRoute, it also returns turn-by-turn directions for every route.

The following is one image of two routes, and a route map for each.

ALL ROUTES
1_Route_1.png  
0_Route_0.png  

 

Here is a button that uses web reference "ts" calling CalculateRoutes method.


 private void button1_Click_6(object sender, EventArgs e)
 {
            btnCalculateRoutes.Enabled = false;
            try
            {
                ts.RoutesSpecification routeRequests = new TestInternal.ts.RoutesSpecification();
                routeRequests.Specifications = new ts.RouteSpecification[2];
                routeRequests.RoutesOptions = new ts.RouteOptions();
                routeRequests.RoutesOptions.MapSize = new ts.MapSize(); // a multi route map
                routeRequests.RoutesOptions.MapSize.Width = 800;    
                routeRequests.RoutesOptions.MapSize.Height = 600;

                for (int j = 0; j < 2; j++)
                {
                    routeRequest = new TestInternal.ts.RouteSpecification();
                    routeRequest.RouteOptions = new ts.RouteOptions();
                    routeRequest.RouteOptions.DistanceUnit = ts.DistanceUnit.Mile;
                    routeRequest.RouteOptions.MapSize = new ts.MapSize();   // individual route map
                    routeRequest.RouteOptions.MapSize.Height = 200;
                    routeRequest.RouteOptions.MapSize.Width = 400;

                    int count = 2;
                    routeRequest.Locations = new TestInternal.ts.Location[count];

                    Random rnd = new Random();
                    for (int i = 0; i < count; i++)
                    {
                        TestInternal.ts.Location loc = new TestInternal.ts.Location();
                        loc.Name = string.Format("Loc_{0}", i + 1);

                        // in this sample we use latitude & longitude but you can use the
                        // Address property of location to geocode your location on-the-fly.

                        loc.LatLong = new TestInternal.ts.LatLong();

                        if (j == 0)
                        {
                            routeRequest.RouteOptions.RouteColor = new ts.RouteColor();
                            routeRequest.RouteOptions.RouteColor.A = 168;
                            routeRequest.RouteOptions.RouteColor.R = 255;
                            routeRequest.RouteOptions.RouteColor.G = 0;
                            routeRequest.RouteOptions.RouteColor.B = 0;

                            if (i == 0)
                            {
                                loc.LatLong.Latitude = 38.951594;
                                loc.LatLong.Longitude = -77.29602;
                            }
                            else
                            {
                                loc.LatLong.Latitude = 38.908467;
                                loc.LatLong.Longitude = -77.064874;
                            }
                        }
                        else
                        {
                            if (i == 0)
                            {
                                loc.LatLong.Latitude = 38.934594;
                                loc.LatLong.Longitude = -77.28602;
                            }
                            else
                            {
                                loc.LatLong.Latitude = 38.948467;
                                loc.LatLong.Longitude = -77.136874;
                            }

                        }
                        routeRequest.Locations[i] = loc;
                    }
                    routeRequests.Specifications[j] = routeRequest;
                }

                try
                {
                    StringBuilder sb = new StringBuilder();
                    string path = "c:\\temp\\route";
                    if (Directory.Exists(path))
                        Directory.Delete(path, true);
                    Directory.CreateDirectory(path);

                    client.Timeout = 1000 * 60 * 30;//30 minutes
                    TestInternal.ts.RoutesResult results = client.CalculateRoutes(routeRequests);
                    for (int k = 0; k < results.Results.Count(); k++)
                    {
                        TestInternal.ts.RouteResult result = results.Results[k];
                        if (result.Status == TestInternal.ts.OperationStatus.Success)
                        {
                            for (int i = 0; i < result.Route.RouteLegs.Length; i++)
                            {
                                sb.AppendFormat("---------------------Leg:{0}\r\n", i);
                                ts.RouteLeg rl = result.Route.RouteLegs[i];
                                for (int j = 0; j < rl.Itinerary.Items.Length; j++)
                                {
                                    ts.RouteItineraryItem ri = rl.Itinerary.Items[j];
                                    sb.AppendFormat("{0}\t [{1:f2},{2}]\r\n", ri.Text, ri.Distance, ri.Time);
                                }
                                if (rl.Map != null)         // map of each leg of route
                                {
                                    byte[] bytes = Convert.FromBase64String(rl.Map);
                                    using (MemoryStream ms = new MemoryStream(bytes))
                                    {
                                        Image mapImage = Image.FromStream(ms);
                                        mapImage.Save(path + "\\" + k + "_Leg_" + i + ".png");
                                    }
                                }
                            }
                            if (result.Route.Map != null)  // individual route map
                            {
                                byte[] bytes = Convert.FromBase64String(result.Route.Map);
                                using (MemoryStream ms = new MemoryStream(bytes))
                                {
                                    Image mapImage = Image.FromStream(ms);
                                    mapImage.Save(path + "\\" + k + "_Route_" + k.ToString() + ".png");
                                }
                            }
                            txtResult.Text = sb.ToString();
                        }
                        else//(result.Status == WebRef.ts.OperationStatus.SuccessWithErrors)
                        {
                            sb.AppendFormat("Status:{0}\r\n", result.Status);
                            foreach (TestInternal.ts.Error i in result.Errors)
                            {
                                sb.AppendFormat("{0}\r\n", i.Message);
                            }
                            txtResult.Text = sb.ToString();
                            //Show result with error. Some of locations can be skipped or may heppens some errors.
                        }
                    }
                    if (results.Map != null)  // this is multi route map
                    {
                        byte[] bytes = Convert.FromBase64String(results.Map);
                        using (MemoryStream ms = new MemoryStream(bytes))
                        {
                            Image mapImage = Image.FromStream(ms);
                            mapImage.Save(path + "\\Routes.png");
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }

            }
            finally
            {
                btnCalculateRoutes.Enabled = true;

            }

        }
    }