CalculateRoutes

 

Use CalculateRoutes to create maps and turn by turn directions fro multiple routes.

 

        private void btnCalculateRoutes_Click(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 some errors may happen.
                        }
                    }
                    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;
            }

        }