Example T1263357
Visible to All Users

WinForms Maps - Use the Azure Maps Route Service to Calculate Routes between GeoPoints on a Map Surface

This example calculates a route based on two or more RouteWaypoint objects.

NOTE:
To incorporate this solution within your DevExpress-powered app, you need an Azure Maps service key. Replace the AzureKey property value with your subscription key to connect to Azure Maps.

Implementation Details

Right-click on a map surface to specify waypoints: origin, destination, and points in between. ListBox entries display geographical points (GeoPoint.Longitude and GeoPoint.Latitude). The “Calculate Route” button initiates the routing request by obtaining waypoint information and passing it to the CalculateRoute method. ComboBoxEdit and CheckedListBox editors specify route options (AzureRouteOptions.AvoidTypes and AzureRouteOptions.TravelMode properties).

Output:

Map

Files to Look At

Documentation

More Examples

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

Form1.cs(vb)
C#
using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Drawing; using System.Linq; using System.Windows.Forms; using DevExpress.XtraMap; namespace AzureRouting { public partial class Form1 : Form { const string azureKey = "your key"; ObservableCollection<GeoPoint> geoPoints = new ObservableCollection<GeoPoint>(); MapItemStorage itemData; InformationLayer routeInfoLayer; AzureRouteDataProvider azureRoute; public Form1() { InitializeComponent(); mapControl.Layers.AddRange( new LayerBase[] { new ImageLayer() { DataProvider = new AzureMapDataProvider() { AzureKey = azureKey, Tileset = AzureTileset.Imagery, }, }, new ImageLayer() { DataProvider = new AzureMapDataProvider() { AzureKey = azureKey, Tileset = AzureTileset.BaseHybridRoad, }, }, routeInfoLayer = new InformationLayer() { DataProvider = azureRoute = new AzureRouteDataProvider() { AzureKey = azureKey, }, }, new VectorItemsLayer() { Data = itemData = new MapItemStorage(), } } ); routeInfoLayer.ItemStyle.StrokeWidth = 2; routeInfoLayer.ItemStyle.Stroke = Color.DeepSkyBlue; routeInfoLayer.Error += RouteInfoLayer_Error; waypointsListBoxControl.DataSource = geoPoints; mapControl.Zoom(6); mapControl.SetCenterPoint(new GeoPoint(40.714627, -74.002863), false); mapControl.EnableRotation = false; } private void RouteInfoLayer_Error(object sender, MapErrorEventArgs e) { throw new System.NotImplementedException(); } private void mapControl_Click(object sender, System.EventArgs e) { MouseEventArgs mouseEventArgs = (MouseEventArgs)e; if(mouseEventArgs.Button == MouseButtons.Right) { GeoPoint mousePoint = mapControl.ScreenPointToCoordPoint(mouseEventArgs.Location) as GeoPoint; geoPoints.Add(mousePoint); itemData.Items.Add(new MapPushpin() { Location = mousePoint, }); } } private void calculateRouteButton_Click(object sender, System.EventArgs e) { if (geoPoints.Count <= 1) { MessageBox.Show("Specify at least two Waypoints to calculate a route."); return; } azureRoute.CalculateRoute(geoPoints.Select(point => new RouteWaypoint("", point)).ToList(), new AzureRouteOptions() { TravelMode = GetTravelMode(), AvoidTypes = GetAvoidTypes() }); geoPoints.Clear(); itemData.Items.Clear(); } AzureTravelMode GetTravelMode() { return (AzureTravelMode)Enum.Parse(typeof(AzureTravelMode), (string)travelModeComboBox.SelectedItem); } AzureRouteAvoidType GetAvoidTypes() { var avoidTypes = AzureRouteAvoidType.None; foreach (string item in avoidTypesCheckedListBox.CheckedItems) { var avoidType = (AzureRouteAvoidType)Enum.Parse(typeof(AzureRouteAvoidType), item); avoidTypes |= avoidType; } return avoidTypes; } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.