2013-03-08 120 views
2

我正在使用Google Maps API v3,並試圖提供「顯示下一個路由」按鈕,用戶可以在其中循環所有可用路由。Google Maps API v3:routeIndex不工作?

我得到它顯示第一個路線,但是當我使用DirectionsRenderer類的routeIndex屬性時,它只返回第一個路線。

我做錯了什麼?代碼粘貼在下面。我一直使用的測試路線共有三條路線。如果我將routeIndex屬性設置爲1,它仍然顯示數組中的第一個路由(基本上,routeIndex [0])。

function showDirections() { 
// show contact buttons 
$(".contact-route-button").css({display:"block"}); 

// add 1 to count 
count++; 

// If this function has been run before, clear the directions 
if(count > 1){ 
    // Clear map 
    directionsDisplay.setMap(null); 
    //Clear Route List 
    document.getElementById('directions').innerHTML = ""; 
} 

// Set map to render directions 
directionsDisplay = new google.maps.DirectionsRenderer({ 
    map: map, 
    /*preserveViewport: true,*/ 
    draggable: true, 
    routeIndex: 1 
}); 

// Remove hidden class form text explaning driving directions 
$('#drive-text').fadeIn("fast").removeClass('hidden'); 

// Hide paragraph under directions form 
$(".section.grids-two.maximum-780.clearfix .grid.grid-2").fadeOut("fast"); 

// SlideToggle panel about random fact 
$(".random-fact").slideDown("fast").css({display:"block"}); 

// Set Panel that will display driving directions 
directionsDisplay.setPanel(document.getElementById('directions')); 

// Get address input text 
var address = document.getElementById('dir-address').value; 

// Create request to send to Google starting at the address provided 
var request = { 
    origin: address, 
    destination: '531 E Market Street Indianapolis, IN 46204', 
    travelMode: google.maps.TravelMode.DRIVING, 
    unitSystem: google.maps.UnitSystem.STANDARD, 
    provideRouteAlternatives: true 
}; 


// Send request and display on map and directions box 
directionsService.route(request, function(response, status) { 
    if (status === google.maps.DirectionsStatus.OK) { 
     directionsDisplay.setDirections(response); 
     totalRoutes = countRoutes(response); 
    } 
}); 
} 

任何想法?我遵循的說明在谷歌地圖API文檔上,https://developers.google.com/maps/documentation/javascript/reference#DirectionsRendererOptions

在此先感謝!

回答

0

看起來routeIndex僅在directionsRenderer中有屬性可用時才起作用。您可以使用:

directionsDisplay.setOptions({directions:response,routeIndex:1}); 

...什麼可以同時設置,directionsrouteIndex

+0

我會嘗試。 謝謝! – dauble 2013-03-12 13:20:46