好像routeService.CalculateRoute(routeRequest)
没有生效routeRequest.Options
。我尝试过设置RouteOptimization.MinimizeDistance
,RouteOptimization.MinimizeTime;
但似乎都没有调整到最佳路线。似乎CalculateRoute 总是根据它们在航点数组中的顺序来计算航点。有谁知道我做错了什么?
Waypoint[] waypoints = new Waypoint[waypointInfo.Count + 1]; // +1 for the store
waypoints[0] = new Waypoint();
waypoints[0].Description = "Store";
waypoints[0].Location = new RouteService.Location();
waypoints[0].Location.Latitude = store.Latitude;
waypoints[0].Location.Longitude = store.Longitude;
for (int i = 0; i < waypointInfo.Count; i++)
{
waypoints[i+1] = new Waypoint();
waypoints[i + 1].Description = waypointInfo[i].Address+ " - " +waypointInfo[i].Name;
waypoints[i+1].Location = new RouteService.Location();
waypoints[i + 1].Location.Latitude = waypointInfo[i].locations.Latitude;
waypoints[i + 1].Location.Longitude = waypointInfo[i].locations.Longitude;
}
RouteRequest routeRequest = new RouteRequest();
routeRequest.Credentials = new Microsoft.Maps.MapControl.WPF.Credentials();
routeRequest.Credentials.ApplicationId = "BING API KEY";
// You want minimum distance or minimum time?
RouteOptions myROuteOptions = new RouteOptions();
myROuteOptions.Mode = TravelMode.Driving;
myROuteOptions.Optimization = RouteOptimization.MinimizeDistance;
myROuteOptions.TrafficUsage = TrafficUsage.TrafficBasedTime;
routeRequest.Options = myROuteOptions;
//routeRequest.UserProfile = new UserProfile { DistanceUnit = RouteService.DistanceUnit.Kilometer };
routeRequest.Waypoints = waypoints;
// Make the calculate route request
RouteServiceClient routeService = new RouteServiceClient();
RouteResponse routeResponse = routeService.CalculateRoute(routeRequest);
double dayDistance = 0;
directions.Clear();
if (routeResponse.Result.Legs.Length > 0)
{
int instructionCount = 0;
int legCount = 0;
foreach (RouteLeg leg in routeResponse.Result.Legs)
{
legCount++;
if (legCount > 1)
{
directions.Append(" " + "\n");
}
foreach (ItineraryItem item in leg.Itinerary)
{
instructionCount++;
directions.Append(string.Format("{0}. {1} {2}\n",
instructionCount, item.Summary.Distance, item.Text));
dayDistance += item.Summary.Distance;
}
}
}