2016-09-29 55 views
0

我正在嘗試新的Bing地圖v8。我遵循此示例:http://www.bing.com/api/maps/sdk/mapcontrol/isdk#directionsCreateTransitRoute+JS必應地圖v8 getRouteResult返回undefined

我需要獲取路由結果(directionsManager.getRouteResult()),但返回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` 
}); 

回答

2

您在異步方向計算完成之前請求路由結果。

您的移動電話getRouteResult進入處理程序directionsManager對象的directionsUpdated事件,像這樣:

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

你必須在directionsEvent對象訪問相同的屬性。

+0

這是有效的。非常感謝! –