Dispatch

Dispatch() method can optimize 100's of stops using one or several vehicles.  For simplicty, this code uses onlye one vehicle and one stop.  We also use lat/lon to set vehicle and location position but you can also use an address.  Dispatch method will automatically geocode address if one used instead of lat/lon.

private void btnDispatchLatLon_Click(object sender, EventArgs e)
        {
            btnDispatchLatLon.Enabled = false;
            try
            {
                //create request if null

                dispatchRequest = new TestInternal.ts.DispatchSpecification();

                //////////////////////////////////////////////////////////////////////
                // Add one vehicle and use start/finish address of vehicle
                dispatchRequest.Vehicles = new TestInternal.ts.Vehicle[1];
                TestInternal.ts.Vehicle v = new TestInternal.ts.Vehicle();

                v.Name = "V1";  // vehicle's name
                v.StartLocation = new TestInternal.ts.Location();
                v.StartLocation.LatLong = new ts.LatLong();
                v.StartLocation.LatLong.Latitude = 41.700154;
                v.StartLocation.LatLong.Longitude = -87.574963;

                v.FinishLocation = new TestInternal.ts.Location();
                v.FinishLocation.LatLong = new ts.LatLong();
                v.FinishLocation.LatLong.Latitude = 41.700154;
                v.FinishLocation.LatLong.Longitude = -87.574963;

                dispatchRequest.Vehicles[0] = v;    // assign to request object
                //////////////////////////////////////////////////////////////////////////

                //Set time to min. value if we do not need dispatching by time.
                dispatchRequest.CurrentTime = DateTime.MinValue;
                dispatchRequest.DispatchMode = ts.DispatchMode.Auto;  //Set dispatch mode.   
                //Set distance unit;
                dispatchRequest.DistanceUnit = ts.DistanceUnit.Mile;

                dispatchRequest.MinimumOptimization = 4;   //Set optimization precision. 

                // create of locations
                int count = 1;

                //Create an array of locations - one location in this sample
                dispatchRequest.Locations = new TestInternal.ts.Location[count];

                TestInternal.ts.Location loc = new TestInternal.ts.Location();
                loc.Name = "Brussel";
                loc.LatLong = new TestInternal.ts.LatLong();
                loc.LatLong.Latitude = 41.859796;
                loc.LatLong.Longitude = -87.620842;
                loc.LocationType = ts.LocationType.Midway;
                dispatchRequest.Locations[0] = loc;

                try
                {
                    client.Timeout = 1000 * 60 * 30;//30 minutes
                    DateTime dt = DateTime.Now;
                    TestInternal.ts.DispatchResult result = client.Dispatch(dispatchRequest);
                    TimeSpan tsp = DateTime.Now.Subtract(dt);
                    if (result.Status == TestInternal.ts.OperationStatus.Success)
                    {
                        StringBuilder sb = new StringBuilder();
                        sb.AppendFormat("Total Time:{0}\r\n\r\n", tsp);
                        //show result
                        for (int i = 0; i < result.Items.Length; i++)
                        {
                            TestInternal.ts.VehicleItem item = result.Items[i];
                            if (item.Vehicle != null)
                                sb.AppendFormat("Vehicle:{0}\r\n", item.Vehicle.Name);
                            //more info....

                            for (int j = 0; j < item.Locations.Length; j++)
                            {
                                TestInternal.ts.Location locr = item.Locations[j];
                                sb.AppendFormat("{0}\t [{1:f5},{2:f5}, {3:f2}, {4}, {5}]\r\n", locr.Name, locr.LatLong.Latitude, locr.LatLong.Longitude, locr.Distance, locr.Time, locr.Wait);
                            }
                        }

                        System.Diagnostics.Debug.WriteLine(sb.ToString());
                        txtResult.Text = sb.ToString();
                        return;
                    }
                    else//(result.Status == WebRef.ts.OperationStatus.SuccessWithErrors) 
                    {

                        StringBuilder sb = new StringBuilder();
                        sb.AppendFormat("Total Time:{0}\r\n\r\n", tsp);
                        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.
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
            finally
            {
                btnDispatchLatLon.Enabled = true;
                //client.Dispatch(
            }
        }