Skip to main content

DevExpress v25.1 Update — Your Feedback Matters

Our What's New in v25.1 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

BingRouteDataProvider Class

OBSOLETE

This class is obsolete. Microsoft deprecated Bing Maps on June 30th, 2025. For alternative providers, see https://www.devexpress.com/bing-maps-winforms.

The class that is used to send requests to the Bing Maps Route service.

Namespace: DevExpress.XtraMap

Assembly: DevExpress.XtraMap.v25.1.dll

NuGet Package: DevExpress.Win.Map

#Declaration

[Obsolete("This class is obsolete. Microsoft deprecated Bing Maps on June 30th, 2025. For alternative providers, see https://www.devexpress.com/bing-maps-winforms.")]
[PreferableMember("BingRouteDataProvider", "", "")]
public class BingRouteDataProvider :
    BingMapDataProviderBase

#Remarks

The Bing Route Data provider is represented by the BingRouteDataProvider object that can be accessed via the InformationLayer.DataProvider property.

Important

On May 21, 2024, Microsoft announced that Bing Maps for Enterprise and its API will be discontinued. Azure Maps will be a single unified enterprise mapping platform available from Microsoft.

To obtain and display map data from Azure Maps, we implemented the following providers:

For information on how to migrate your app from Bing Maps to Azure Maps, see the following help topic: Migrate from Bing Maps to Azure Maps Providers.

If you already have a Bing Maps for Enterprise license, you can keep using the current API. You must transition to the new API by June 30, 2025 (for free/basic licenses) or June 30, 2028 (for enterprise licenses). New licenses will no longer be available after June 30, 2025. Bing Maps will not work with our map controls without a license after that date.

To access the route options of the Bing Route Data provider, use the BingRouteDataProvider.RouteOptions property.

Note

The BingRouteDataProvider.CalculateRoute method calculates the route based on less than 265 RouteWaypoint objects.

#Example

This example demonstrates how to calculate routes to the destination point from major roads. For this do the following.

  • To calculate routes from major roads the BingRouteDataProvider.CalculateRoutesFromMajorRoads method should be called. In this example it is called in the Calculate Routes From Major Roads button click event handler.
  • To process request result the BingRouteDataProvider.RouteCalculated event should be handled. The requested results contain the total distance of a route, itinerary item (BingRouteResult.Distance, BingRouteLeg.Distance, BingItineraryItem.Distance), the time required to follow the calculated route (BingRouteResult.Time) and pass the route leg and itinerary item (BingRouteLeg.Time, BingItineraryItem.Time). You can also see the maneuvers associated with the itinerary item (BingItineraryItem.Maneuver) and other parameters.
  • To customize map items generated by the BingRouteDataProvider object (because the InformationDataProviderBase.GenerateLayerItems property value is true by default) the InformationDataProviderBase.LayerItemsGenerating event should be handled.

View Example

using System;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraMap;

namespace CalculateRoutesFromMajorRoads {
    public partial class Form1 : Form {
        const double minLat = -90;
        const double maxLat = 90;
        const double minLon = -180;
        const double maxLon = 180;

        const string yourBingKey = "Insert Your Bing Key Here";//"Your Bing Key";

        double lat = 40;
        double lon = -120;
        BingRouteDataProvider routeProvider;

        public Form1() {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e) {
            imageProvider.BingKey = yourBingKey;

            routeProvider = new BingRouteDataProvider { BingKey = yourBingKey };
            routeProvider.LayerItemsGenerating += OnLayerItemsGenerating;
            routeProvider.RouteCalculated += OnRouteCalculated;
            informationLayer.DataProvider = routeProvider;
            informationLayer.DataRequestCompleted += OnDataRequestCompleted;

            teLatitude.Text = lat.ToString();
            teLongitude.Text = lon.ToString();
            #region #InitBingRouteOptions
            cbeTravelMode.Properties.Items.AddRange(Enum.GetValues(typeof(BingTravelMode)));
            cbeTravelMode.SelectedIndex = 0;
            cbeRouteOptimization.Properties.Items.AddRange(Enum.GetValues(typeof(BingRouteOptimization)));
            cbeRouteOptimization.SelectedIndex = 0;
            #endregion #InitBingRouteOptions
        }

        #region #LayerItemsGenerating
        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;
                }
            }
        }
        #endregion #LayerItemsGenerating

        #region #DataRequestCompleted
        void OnDataRequestCompleted(object sender, RequestCompletedEventArgs e) {
            mapControl.ZoomToFitLayerItems(0.4);
        }
        #endregion #DataRequestCompleted

        #region #BingRouteOptionsChanged
        private void OnTravelModeSelectedIndexChanged(object sender, EventArgs e) {
            routeProvider.RouteOptions.Mode = (BingTravelMode)cbeTravelMode.SelectedItem;
        }

        private void OnRouteOptimizationSelectedIndexChanged(object sender, EventArgs e) {
            routeProvider.RouteOptions.RouteOptimization = (BingRouteOptimization)cbeRouteOptimization.SelectedItem;
        }
        #endregion #BingRouteOptionsChanged

        void ShowErrorMessage(string variable, double minVal, double maxVal) {
            MessageBox.Show(
                        this,
                        String.Format(
                            "The {0} value shoud be larger or equals {1} and less or equals {2}.",
                            variable,
                            minVal,
                            maxVal
                        ),
                        variable
                    );
        }

        private void OnLatitudeValidating(object sender, CancelEventArgs e) {
            if(!Double.TryParse(teLatitude.Text, out lat) ||
                (lat > maxLat) || (lat < minLat)) {
                e.Cancel = true;
                ShowErrorMessage("Latitude", minLat, maxLat);
            }
        }

        private void OnLongitudeValidating(object sender, CancelEventArgs e) {
            if(!Double.TryParse(teLongitude.Text, out lon) ||
                (lon > maxLon) || (lon < minLon)) {
                e.Cancel = true;
                ShowErrorMessage("Longitude", minLon, maxLon);
            }
        }

        private void OnCalculateRoutesClick(object sender, EventArgs e) {
            RouteWaypoint targetPoint = new RouteWaypoint("Searched location", new GeoPoint(lat, lon));
            routeProvider.CalculateRoutesFromMajorRoads(targetPoint);
        }

        #region #RouteCalculated
        private void OnRouteCalculated(object sender, BingRouteCalculatedEventArgs e) {
            RouteCalculationResult result = e.CalculationResult;
            if((result.RouteResults == null) ||
                (result.ResultCode == RequestResultCode.BadRequest)) {
                rtbResults.Text = "The Bing Route service does not work for this location.";
                return;
            }

            StringBuilder resultList = new StringBuilder("");

            if(result.IntermediatePoints != null) {
                resultList.Append(String.Format("_________________________\n"));

                for(int i = 0; i < e.CalculationResult.IntermediatePoints.Count; i++)
                    resultList.Append(
                        String.Format("Starting point {0}: {1} ({2})\n",
                        i + 1,
                        e.CalculationResult.IntermediatePoints[i].Description,
                        e.CalculationResult.IntermediatePoints[i].Location)
                    );
            }

            if((result.RouteResults != null) & (result.ResultCode == RequestResultCode.Success)) {

                for(int rnum = 0; rnum < e.CalculationResult.RouteResults.Count; rnum++) {
                    resultList.Append(String.Format("_________________________\n"));
                    resultList.Append(String.Format("Path {0}:\n", rnum + 1));
                    resultList.Append(String.Format(
                        "Distance: {0}\n",
                        e.CalculationResult.RouteResults[rnum].Distance
                    ));
                    resultList.Append(String.Format(
                        "Time: {0}\n",
                        e.CalculationResult.RouteResults[0].Time
                    ));

                    if(e.CalculationResult.RouteResults[rnum].Legs != null) {
                        int legNum = 1;
                        foreach(BingRouteLeg leg in e.CalculationResult.RouteResults[rnum].Legs) {
                            resultList.Append(String.Format("\tLeg {0}:\n", legNum++));
                            resultList.Append(String.Format("\tDistance: {0}\n", leg.Distance));
                            resultList.Append(String.Format("\tTime: {0}\n", leg.Time));
                            if(leg.Itinerary != null) {
                                foreach(BingItineraryItem itineraryItem in leg.Itinerary) {
                                    resultList.Append(String.Format(itineraryItem.Maneuver + "\n"));
                                    resultList.Append(String.Format(
                                        "\t\tLocation: {0}\n",
                                        itineraryItem.Location
                                    ));
                                }
                            }
                        }
                    }
                }
            }
            rtbResults.Text = resultList.ToString();
        }
        #endregion #RouteCalculated
    }
}

#Implements

See Also