Bing Route Optimization对routeS service.CalculateRoute没有任何影响

时间:2013-07-17 17:34:17

标签: c# wpf bing-maps

routeService.CalculateRoute(routeRequest)似乎没有生效routeRequest.Options。我尝试过设置RouteOptimization.MinimizeDistanceRouteOptimization.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;
                    }
                }

}

3 个答案:

答案 0 :(得分:1)

您使用的

The waypoints完全按照您的说法行事。他们对Bing说,路线应该按此顺序排列。所以它完美无缺,你没有做错任何事。

http://msdn.microsoft.com/en-us/library/cc966873.aspx

答案 1 :(得分:1)

我认为CalculateRoute不会重新订购航点。 RouteOptimization仅影响在指定路线中任意两个连续航路点之间选择的路径。

答案 2 :(得分:0)

在你的情况下,如果你没有在&#34;开始&#34;之间给出过多的航路点,你就可以确定路线必须通过的点数。和&#34;结束&#34;你可以看到优化的不同。

我尝试了你的代码,但它说我必须将一个Endpoint作为参数传递给:

RouteServiceClient routeService = new RouteServiceClient("Here");

知道如何解决这个问题