Routing in Virtual Earth Web Service

I’ve been working on this one for a while and it kind of completes the basics for getting started with the Virtual Earth Web Service. The Routing Service iallows for route calculation and itinerary creation.

There are really two ways leverage the routing features of Bing Maps for Enterprise, directly through the REST APIs as is documented at Bing Maps Routes API or using the built in Direction Module in the Bing Maps V8 Web Control.  As looking at JSON & XML results are not as flashy as seeing it live, the following will get you started using the routing service within the Bing Maps V8 control by leveraging the Directions Module so you can see the API results in action. 

First things first, instantiate your map.

     map = new Microsoft.Maps.Map('#myMap', { 
        credentials: 'Your Bing Maps Key' 
     });

Next, load the Directions Module with the route waypoints and options you want for your route including route type and display options like line color. How about a trip from London to Madrid with a stop in Paris?

        //Load the directions module.
        Microsoft.Maps.loadModule('Microsoft.Maps.Directions', function () {
            //Create an instance of the directions manager.
            directionsManager = new Microsoft.Maps.Directions.DirectionsManager(map);

            //Create waypoints to route between.
            directionsManager.addWaypoint(new Microsoft.Maps.Directions.Waypoint({ address: 'London, UK' }));
            directionsManager.addWaypoint(new Microsoft.Maps.Directions.Waypoint({ address: 'Paris, FR' }));
            directionsManager.addWaypoint(new Microsoft.Maps.Directions.Waypoint({ address: 'Madrid, ES' }));

            //Set the request options that avoid highways and uses kilometers.
            directionsManager.setRequestOptions({
                distanceUnit: Microsoft.Maps.Directions.DistanceUnit.km,
                routeAvoidance: [Microsoft.Maps.Directions.RouteAvoidance.avoidLimitedAccessHighway]
            });

            //Make the route line thick and green. Replace the title of waypoints 
            //with an empty string to hide the default text that appears.
            directionsManager.setRenderOptions({
                drivingPolylineOptions: {
                    strokeColor: 'green',
                    strokeThickness: 6
                },
                waypointPushpinOptions: {
                    title: ''
                }
            });

            //Calculate directions.
            directionsManager.calculateDirections();
        });

Running this code will display a map with a route from London, UK to Madrid, Spain, going through Paris, France which looks like this:

Bing Maps V8 with Driving Directions

Pretty simple, but hopefully this will provide you with the information you need to get your routing application up and running. I didn’t do error handling, so make sure you include that with your application. Specifically, when you’re setting up your service calls!