Skip to main content
All docs
V24.1

BingRouteOptions.OptimizeWaypoints Property

Specifies whether the service rearranges route waypoints to reduce route cost.

Namespace: DevExpress.XtraMap

Assembly: DevExpress.XtraMap.v24.1.dll

NuGet Package: DevExpress.Win.Map

Declaration

[DefaultValue(false)]
public bool OptimizeWaypoints { get; set; }

Property Value

Type Default Description
Boolean false

true if waypoints are rearranged; otherwise, false.

Property Paths

You can access this nested property as listed below:

Object Type Path to OptimizeWaypoints
BingRouteDataProvider
.RouteOptions .OptimizeWaypoints

Remarks

Enable the OptimizeWaypoints property to rearrange waypoints passed to the BingRouteDataProvider.CalculateRoute method to create an optimized route.

The following images depict non-optimized and optimized routes for six waypoints (“San Francisco”, “Las Vegas”, “San Jose”, “Chico”, “Los Angeles”, “Oakland”):

OptimizeWaypoints = false
A non-optimized route.
OptimizeWaypoints = true
An optimized route.

The OptimizeWaypoints option is available only for Driving mode (Mode is set to Driving). The list of waypoints should include more than 2 points.

Use the BingRouteResult.WaypointsOrder property to obtain optimized waypoint indexes. Note that the first and last waypoint positions do not change when waypoints are optimized.

You can also use the RouteOptimization property to optimize the route in addition to waypoints.

Example

The following code creates an optimized route for a list of waypoints:

using DevExpress.XtraMap;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace RouteCalculationSample {
    public partial class Form1 : Form {

        private void Form1_Load(object sender, EventArgs e) {
            // Create a background image layer.
            ImageLayer imageLayer = new ImageLayer();
            imageLayer.DataProvider = new BingMapDataProvider {
                BingKey = "Insert your Bing key.",
                Kind = BingMapKind.RoadLight
            };
            mapControl1.Layers.Add(imageLayer);

            // Create an information layer and add it to the map control.
            InformationLayer infoLayer = new InformationLayer();
            mapControl1.Layers.Add(infoLayer);

            // Create a BingRouteDataProvider.
            BingRouteDataProvider routeProvider = new BingRouteDataProvider();
            // Specify the provider's Bing key.
            routeProvider.BingKey = "Insert your Bing key.";
            // Create a list of waypoints.
            List<RouteWaypoint> waypoints = new List<RouteWaypoint>() {
                new RouteWaypoint("San Francisco", new GeoPoint( 37.4639, -122.2459)),
                new RouteWaypoint("Las Vegas", new GeoPoint( 36.1030, -115.0811)),
                new RouteWaypoint("San Jose", new GeoPoint( 37.20, -121.54)),
                new RouteWaypoint("Chico", new GeoPoint(39.4424, -121.508)),
                new RouteWaypoint("Los Angeles", new GeoPoint( 34.03, -118.15)),
                new RouteWaypoint("Oakland", new GeoPoint( 37.4816, -122.1615))
            };
            // Notify the provider that the requested route should be optimized.
            routeProvider.RouteOptions.OptimizeWaypoints = true;
            // Enable Driving mode.
            routeProvider.RouteOptions.Mode = BingTravelMode.Driving;
            // Calculate the route for specified waypoints.
            routeProvider.CalculateRoute(waypoints);
            // Handle the LayerItemsGenerating event to customize the route polyline appearance.
            routeProvider.LayerItemsGenerating += OnLayerItemsGenerating;
            // Assign the provider to the information layer.
            infoLayer.DataProvider = routeProvider;
            infoLayer.DataRequestCompleted += OnDataRequestCompleted;
        }

        private void OnLayerItemsGenerating(object sender, LayerItemsGeneratingEventArgs e) {
            foreach (MapItem item in e.Items) {
                MapPolyline polyline = item as MapPolyline;
                if (polyline != null) {
                    polyline.Stroke = Color.FromArgb(0xFF, 0x00, 0x72, 0xC6);
                    polyline.StrokeWidth = 4;
                }
            }
        }
        private void OnDataRequestCompleted(object sender, RequestCompletedEventArgs e) {
            // Call the ZoomToFitLayerItems method to zoom the map so that it displays the whole generated route.
            mapControl1.ZoomToFitLayerItems(0.4);
        }
    }
}
See Also