Back to Devexpress

How to: Calculate a Route by Addresses

windowsforms-116116-controls-and-libraries-map-control-examples-gis-data-routing-how-to-calculate-a-route-by-addresses.md

latest4.1 KB
Original Source

How to: Calculate a Route by Addresses

  • Nov 13, 2018
  • 2 minutes to read

To send a route request, use the BingRouteDataProvider.CalculateRoute method. This method receives a list of RouteWaypoint objects as a parameter. Note that RouteWaypoint objects are created using the constructor that receives the waypoint description and a keyword that is used to search for a location on a map.

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

namespace ZoomToFitOnRouteCalculated {
    public partial class Form1 : Form {
        const String yourBingKey = "Insert Your Key Here";

        public Form1() {
            InitializeComponent();
            imageProvider.BingKey = yourBingKey;
        }

        private void Form1_Load(object sender, EventArgs e) {
            // Create a new route provider.
            BingRouteDataProvider provider = new BingRouteDataProvider { BingKey = yourBingKey };
            informationLayer.DataProvider = provider;

            // Send a request and handle request result.
            provider.RouteCalculated += OnRouteCalculated;
            // Note that waypoints are created using address instead of the location.
            provider.CalculateRoute(new List<RouteWaypoint>() {
                new RouteWaypoint("New York", "Belmont Park, New York, USA"),
                new RouteWaypoint("Las Vegas", "Lorenzi Park, Las Vegas, USA")});
            splashScreenManager1.ShowWaitForm();
        }

        void OnRouteCalculated(object sender, BingRouteCalculatedEventArgs e) {
            SearchBoundingBox box = e.CalculationResult.RouteResults[0].BoundingBox;
            GeoPoint topLeft = new GeoPoint {
                Latitude = box.NorthLatitude,
                Longitude = box.WestLongitude
            };
            GeoPoint bottomRight = new GeoPoint {
                Latitude = box.SouthLatitude,
                Longitude = box.EastLongitude
            };
            mapControl.ZoomToRegion(topLeft, bottomRight, 0.4);
            splashScreenManager1.CloseWaitForm();
        }
    }
}
vb
Imports DevExpress.XtraMap
Imports System
Imports System.Collections.Generic
Imports System.Windows.Forms

Namespace ZoomToFitOnRouteCalculated
    Partial Public Class Form1
        Inherits Form

        Private Const yourBingKey As String = "Insert Your Key Here"

        Public Sub New()
            InitializeComponent()
            imageProvider.BingKey = yourBingKey
        End Sub

        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
            ' Create a new route provider.
            Dim provider As BingRouteDataProvider = New BingRouteDataProvider With {.BingKey = yourBingKey}
            informationLayer.DataProvider = provider

            ' Send a request and handle request result.
            AddHandler provider.RouteCalculated, AddressOf OnRouteCalculated
            ' Note that waypoints are created using address instead of the location.
            provider.CalculateRoute(New List(Of RouteWaypoint)() From { _
                New RouteWaypoint("New York", "Belmont Park, New York, USA"), _
                New RouteWaypoint("Las Vegas", "Lorenzi Park, Las Vegas, USA") _
            })
            splashScreenManager1.ShowWaitForm()
        End Sub

        Private Sub OnRouteCalculated(ByVal sender As Object, ByVal e As BingRouteCalculatedEventArgs)
            Dim box As SearchBoundingBox = e.CalculationResult.RouteResults(0).BoundingBox
            Dim topLeft As GeoPoint = New GeoPoint With {.Latitude = box.NorthLatitude, .Longitude = box.WestLongitude}
            Dim bottomRight As GeoPoint = New GeoPoint With {.Latitude = box.SouthLatitude, .Longitude = box.EastLongitude}
            mapControl.ZoomToRegion(topLeft, bottomRight, 0.4)
            splashScreenManager1.CloseWaitForm()
        End Sub
    End Class
End Namespace