Google Maps API v3:routeIndex不起作用? [英] Google Maps API v3: routeIndex Not Working?
问题描述
我正在使用Google Maps API v3,并试图提供一个显示下一条路线"按钮,用户可以在其中循环浏览所有可用路线.
I'm using the Google Maps API v3 and am trying to provide a "Show Next Route" button, where a user can cycle through all available routes.
我已经显示了第一条路线,但是当我使用DirectionsRenderer类的routeIndex属性时,它仅返回第一条路线.
I've got it displaying the first route, but when I use the routeIndex property of the DirectionsRenderer class, it only returns the first route.
我做错什么了吗?代码粘贴在下面.我一直在使用的测试路由共有三条路由.如果将routeIndex属性设置为1,它仍会显示数组中的第一条路由(基本上是routeIndex [0]).
Am I doing something wrong? Code pasted below. The test route I've been using has a total of three routes. If I set the routeIndex property to 1, it still displays the first route in the array (basically, 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);
}
});
}
有什么想法吗?我遵循的说明位于Google Maps API文档中, https://developers .google.com/maps/documentation/javascript/reference#DirectionsRendererOptions
Any ideas? The instructions I'm following are on the Google Maps API documentation, https://developers.google.com/maps/documentation/javascript/reference#DirectionsRendererOptions
提前谢谢!
推荐答案
似乎只有当DirectionsRenderer中的directions
属性可用时,routeIndex
才有效.您可以使用:
It appears that the routeIndex
does only have an effect when the directions
-property is available in the directionsRenderer. You may use:
directionsDisplay.setOptions({directions:response,routeIndex:1});
...将同时设置directions
和routeIndex
...what will set both, directions
and routeIndex
这篇关于Google Maps API v3:routeIndex不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!