Route Optimization API
Route optimization can be performed simply by using the Dispatch() method. Dispatch method takes 2 parameters as input and returns optimized results for each vehicle.
We must attach the web reference to our project:
http://trackservice.trackroad.com/trackservice.asmx?wsdl
First, we create a vehicle with Start/Finish address. The following is a SOAP example.
dispatchRequest = new TestInternal.ts.DispatchSpecification();
//////////////////////////////////////////////////////////////////////
// Add a vehicle and use start/finish address of vehicle to create the route
dispatchRequest.Vehicles = new TestInternal.ts.Vehicle[1];
TestInternal.ts.Vehicle v = new TestInternal.ts.Vehicle();
v.Name = "V1";
v.StartLocation = new TestInternal.ts.Location();
v.StartLocation.Address = new ts.Address();
v.StartLocation.Address.Street = "6718 Whittier Avenue";
v.StartLocation.Address.City = "McLean";
v.StartLocation.Address.State = "VA";
v.StartLocation.Address.PostalCode = "22101";
v.FinishLocation = new TestInternal.ts.Location();
v.FinishLocation.Address = new ts.Address();
v.FinishLocation.Address.Street = "1309 Vincent Place";
v.FinishLocation.Address.City = "McLean";
v.FinishLocation.Address.State = "VA";
v.FinishLocation.Address.PostalCode = "22101";
// assign to request object
dispatchRequest.Vehicles[0] = v;
Set our options:
dispatchRequest.DistanceUnit = TestInternal.ts.DistanceUnit.Mile;
//Set time to min. value if we do not need dispatching by time.
dispatchRequest.CurrentTime = DateTime.MinValue;
//Set dispatch mode. Possible: Auto, EqualStop, SingleRegion, MultipleRegion , etc.
dispatchRequest.DispatchMode = TestInternal.ts.DispatchMode.Auto;
//Set distance unit;
dispatchRequest.DistanceUnit = TestInternal.ts.DistanceUnit.Kilometer;
//Set optimization precision.
dispatchRequest.MinimumOptimization = 4;
dispatchRequest.IsNeedMatchCode = true;
Add two stops
dispatchRequest.Locations = new TestInternal.ts.Location[2];
TestInternal.ts.Location locA = new TestInternal.ts.Location();
locA.Name = "Stop A";
locA.Address = new ts.Address();
locA.Address.City = "McLean";
locA.Address.Street = "6718 Whittier Avenue";
locA.Address.PostalCode = "22101";
dispatchRequest.Locations[0] = locA;
TestInternal.ts.Location locB = new TestInternal.ts.Location();
locB.Name = "Stop B";
locB.Address = new ts.Address();
locB.Address.City = "McLean";
locB.Address.Street = "1309 Vincent Place";
locB.Address.PostalCode = "22101";
dispatchRequest.Locations[1] = locB;
Call server to optimize the stops from start to finish
try
{
client.Timeout = 1000 * 60 * 30;//30 minutes
DateTime dt = DateTime.Now;
TestInternal.ts.DispatchResult result = client.Dispatch(dispatchRequest);
TimeSpan ts = DateTime.Now.Subtract(dt);
if (result.Status == TestInternal.ts.OperationStatus.Success)
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("Total Time:{0}\r\n\r\n", ts);
//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);
for (int j = 0; j < item.Locations.Length; j++)
{
TestInternal.ts.Location loc = item.Locations[j];
sb.AppendFormat("{0}\t Match: {1}\t [{2:f5},{3:f5}, {4}, {5:f2}]\r\n", loc.Name, loc.MatchCode, loc.LatLong.Latitude, loc.LatLong.Longitude, loc.Time, loc.Distance); // time and distance to next location
}
}
txtResult.Text = sb.ToString();
return;
}
else
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("Total Time:{0}\r\n\r\n", ts);
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.
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}