0
votes

I am trying the new Bing Maps v8. I follow this example: http://www.bing.com/api/maps/sdk/mapcontrol/isdk#directionsCreateTransitRoute+JS

I need to get the route result (directionsManager.getRouteResult()), but returns undefined.

var map = new Microsoft.Maps.Map(document.getElementById('rs-mapa-elem'), {
    credentials: 'Your Bing Maps Key',
    center: new Microsoft.Maps.Location(47.606209, -122.332071),
    zoom: 12
});

Microsoft.Maps.loadModule('Microsoft.Maps.Directions', function () {
    var directionsManager = new Microsoft.Maps.Directions.DirectionsManager(map);

    // Set Route Mode to transit
    directionsManager.setRequestOptions({ routeMode: Microsoft.Maps.Directions.RouteMode.transit });

    var waypoint1 = new Microsoft.Maps.Directions.Waypoint({ address: 'Redmond', location: new Microsoft.Maps.Location(47.67683029174805, -122.1099624633789) });
    var waypoint2 = new Microsoft.Maps.Directions.Waypoint({ address: 'Seattle', location: new Microsoft.Maps.Location(47.59977722167969, -122.33458709716797) });

    directionsManager.addWaypoint(waypoint1);
    directionsManager.addWaypoint(waypoint2);

    // Set the element in which the itinerary will be rendered
    directionsManager.setRenderOptions({ itineraryContainer: document.getElementById('printoutPanel') });
    directionsManager.calculateDirections();

   var route =directionsManager.getRouteResult();

   console.log(route); // Returns `undefined`
});
1

1 Answers

3
votes

You're requesting the route result before the asynchronous directions calculation is complete.

Move your getRouteResult call into a handler for the directionsUpdated event of the directionsManager object, like this:

Microsoft.Maps.Events.addHandler(
  directionsManager,
  'directionsUpdated',
  function (directionsEvent)
  {
     var route = directionsManager.getRouteResult();
  });

You'll have access to the same properties in the directionsEvent object.