DirectionsManager Class
Contains methods for calculating directions and displaying a route on a map.
Constructor
| Name | Definition | Description |
|---|---|---|
|
DirectionsManager |
|
Initializes a new instance of the DirectionsManager class. |
Methods
| Name | Definition | Return Value | Description |
|---|---|---|---|
|
addWaypoint |
|
None |
Adds a waypoint to the route at the given index, if specified. If an index is not specified the waypoint is added as the last waypoint in the route. To recalculate the route, use calculateDirections. The maximum number of walking or driving waypoints is 25. The maximum number of transit waypoints is 2. Up to 10 via points are allowed between two stop waypoints. |
|
calculateDirections |
|
None |
Calculates directions based on request and render options set (setRequestOptions, setRenderOptions) and the waypoints added (addWaypoint). The directionsUpdated event fires when the calculation is complete and the route is displayed on the map. You must call this method after making any changes to the route options or waypoints. |
|
clearDisplay |
|
None |
Clears the directions displayed and removes the route line from the map. This method does not remove waypoints from the route and retains all calculated direction information and option settings. To clear the calculated directions and options, use resetDirections. |
|
dispose |
|
None |
Deletes the DirectionsManager object and releases any associated resources. |
|
getAllWaypoints |
|
Waypoint[] |
Returns the waypoints for the route. |
|
getMap |
|
Returns the map object associated with the directions manager. |
|
|
getNearbyMajorRoads |
|
None |
Searches around the specified location for nearby major roads and returns them as an object to the callback function. |
|
getRenderOptions |
|
Returns the route render options. |
|
|
getRequestOptions |
|
Returns the directions request options. |
|
|
getRouteResult |
|
Route[] |
Returns the current calculated route(s). If the route was not successfully calculated, null is returned. |
|
removeWaypoint |
|
None |
Removes the given waypoint or the waypoint identified by the given index from the route. Use calculateDirections to update the route once a waypoint has been removed. |
|
resetDirections |
|
None |
If no options object is supplied, clears the directions request and render options and removes all waypoints. |
|
reverseGeocode |
|
None |
Matches the specified location to an address and returns the address to the specified callback function. |
|
setMapView |
|
None |
Sets the map view based on the current route index. |
|
setRenderOptions |
|
None |
Sets the specified render options for the route. |
|
setRequestOptions |
|
None |
Sets the specified route calculation options. |
Events
| Name | Arguments | Description |
|---|---|---|
|
afterRouteSelectorRender |
Occurs after the route selector has fully rendered. |
|
|
afterStepRender |
Occurs after each step in the itinerary has fully rendered. |
|
|
afterSummaryRender |
Occurs after the route summary has fully rendered. |
|
|
afterWaypointRender |
Occurs after each route waypoint has fully rendered. |
|
|
beforeDisambiguationRender |
Occurs before the waypoint disambiguation element is rendered. Use the autoDisplayDisambiguation directions render option to automatically display waypoint disambiguation. |
|
|
beforeRouteSelectorRender |
Occurs just before the route selector renders. |
|
|
beforeStepRender |
Occurs just before each step in the itinerary renders. |
|
|
beforeSummaryRender |
Occurs just before the route summary renders. |
|
|
beforeWaypointRender |
Occurs just before each route waypoint renders. |
|
|
directionsError |
Occurs when calculating the directions caused an error. |
|
|
directionsUpdated |
Occurs when the directions calculation was successful and the itinerary and route on the map have been updated. |
|
|
dragDropCompleted |
None |
Occurs when the drag of a waypoint or route is completed. |
|
itineraryStepClicked |
Occurs when a step in the itinerary is clicked. |
|
|
mouseEnterRouteSelector |
Occurs when the mouse cursor is over the route selector. |
|
|
mouseEnterStep |
Occurs when the mouse cursor is over a directions step. |
|
|
mouseLeaveRouteSelector |
Occurs when the mouse cursor leaves the route selector. |
|
|
mouseLeaveStep |
Occurs when the mouse cursor leaves a directions step. |
|
|
routeSelectorClicked |
Occurs when the route selector is clicked. |
|
|
waypointAdded |
Occurs when a new waypoint is added to the route. |
|
|
waypointRemoved |
Occurs when a waypoint is removed from the route. |
Example
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script type="text/javascript" src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0"></script>
<script type="text/javascript">
var map = null;
function GetMap()
{
// Initialize the map
map = new Microsoft.Maps.Map(document.getElementById("mapDiv"),{credentials:"Bing Maps Key"});
Microsoft.Maps.loadModule('Microsoft.Maps.Directions', { callback: directionsModuleLoaded });
}
function directionsModuleLoaded()
{
// Initialize the DirectionsManager
var directionsManager = new Microsoft.Maps.Directions.DirectionsManager(map);
// Create start and end waypoints
var startWaypoint = new Microsoft.Maps.Directions.Waypoint({address:"Seattle, WA"});
var endWaypoint = new Microsoft.Maps.Directions.Waypoint({address:"Portland, OR"});
directionsManager.addWaypoint(startWaypoint);
directionsManager.addWaypoint(endWaypoint);
// Set the id of the div to use to display the directions
directionsManager.setRenderOptions({ itineraryContainer: document.getElementById('itineraryDiv') });
// Specify a handler for when an error occurs
Microsoft.Maps.Events.addHandler(directionsManager, 'directionsError', displayError);
// Calculate directions, which displays a route on the map
directionsManager.calculateDirections();
}
function displayError(e)
{
alert(e.message);
}
</script>
</head>
<body onload="GetMap();">
<div id='mapDiv' style="position:relative; width:400px; height:400px;"></div>
<div id='itineraryDiv' style="position:relative; width:400px;"></div>
</body>
</html>