Routing
- 4 minutes to read
This document describes how to use the routing feature in the Map control. This feature allows you to calculate either a route between locations or routes from major roads in four directions on a map.
The document consists of the following sections.
- Overview
- Enable Routing
- Calculate a Route between Locations
- Calculate Routes from Major Roads
- Routing Result
- Examples
#Overview
The Map control supports the popular Microsoft Bing Route service. This service provides the most optimal route, either from major roads in four directions, or calculated between two or more locations on a map.
NOTE
The routing feature should interact with one of the map data providers (Bing Maps or Open
The routing functionality in the Map control is performed by the Bing Route data provider. This provider is represented by the BingRouteDataProvider object. The section below explains how to use the BingRouteDataProvider in the map control.
#Enable Routing
To enable routing in the Map control, do the following.
Create an information layer and add it to the map.
The information layer is used to present GIS elements above the map. For more information, see Layers.
- Create an instance of the BingRouteDataProvider and assign it to the InformationLayer.DataProvider property.
Specify the Bing Maps key via the BingRouteDataProvider.BingKey property.
NOTE
If you run the application, and see a window with the following error message: ”The specified Bing Maps key is invalid. To create a developer account, refer to http://www.
microsoft. , refer to the following tutorial: How to: Get a Bing Maps Key.com/maps/developers”
The code snippet below shows how this can be done.
There are two ways to use the map routing feature in your application, depending on your task.
- calculate a route between two or more locations on a map;
- get a route from major roads on the map.
The sections below describe each approach.
#Calculate a Route between Locations
When the Map control is connected to the Bing Route service (see the section above for details), you can calculate a route between two or more locations on a map. To accomplish this, call the BingRouteDataProvider.CalculateRoute method and pass the list of locations (waypoints) as its argument, as shown below.
The image below shows the calculated route between the specified locations on the map.
#Calculate Routes from Major Roads
The Map control allows you to obtain optimal routes from major roads in four directions for a specified destination location (latitude and longitude coordinates).
For instance, you have a UI that consists of 2 combo boxes named "cbMode" and "cbOptimize", 2 text boxes named "tbLatitude" and "tbLongitude", and a button named "calculateRoutes".
To start a route calculation, click the Calculate Routes button. This calls the BingRouteDataProvider.CalculateRoutesFromMajorRoads method and a destination location (waypoint coordinates and description) and options is passed to its argument.
The results for the San Francisco location (Latitude - "37.783333" and Longitude - "-122.416667") are shown in the image below.
#Routing Result
The results from the Bing Route service are stored by the RequestResultBase object's RouteCalculationResult descendant inside the BingRouteDataProvider.RouteCalculated event handler's BingRouteCalculatedEventArgs.
For instance, you can access a route path between locations calculated in the Calculate a Route between Locations section of this document.
To accomplish this, handle the BingRouteDataProvider.RouteCalculated event.
The result is shown in the image below.
#Examples
The following examples demonstrate how to use the routing feature in the map control.
- How to: Connect to the Bing Route Service
- How to: Get Routes to the Destination Point Using the Bing Route Service