我正在尝试为 48 个航点渲染方向,但谷歌只允许每个请求渲染 8 个航点。所以我做了多个请求(在我的例子中是 6)。但是,API 以某种方式为最后一个请求执行了 fitBounds() 。
如何使用 fitBounds() 和/或 setZoom() 在一张地图中显示所有 48 个航点
我的代码如下: -
function DisplayDirection(directionList){
var interval = 8; // upper bound for usage limits in google directions API is 8
var startIndex =0;
var maxmimumIndex = directionList.length-1; // Total number of waypoints in this route
var partialEndIndex = interval-1; // end waypoint at start
var iteration = 0; // loop controler
directionsService = new google.maps.DirectionsService();
var resultSet = new Array();
var directionsDisplayList = new Array();
var resultsCached = 0;
var bounds = new google.maps.LatLngBounds();
do { //do...while to iterate over multiple requests
iteration++;
if(iteration>1){
startIndex = startIndex+interval;
partialEndIndex = startIndex+interval;
}
var directionsDisplay = new google.maps.DirectionsRenderer({
markerOptions: {
visible:false
}
});
directionsDisplayList.push(
directionsDisplay
);
directionsDisplayList[iteration-1].setMap(map);
var origin = directionList[startIndex];
var destination = partialEndIndex < maxmimumIndex ? directionList[partialEndIndex]:directionList[maxmimumIndex];
waypoints = new Array();
for(var i=startIndex+1;i<partialEndIndex;i++){
if(i>maxmimumIndex){
break;
}
waypoints.push(
{
location:directionList[i],
stopover:true
}
);
latlong = directionList[i].split(","); //
lat = latlong[0]; //
lng = latlong[1]; //
bounds.extend (new google.maps.LatLng(lat,lng)); //extend the bounds
}
var request = {
origin: origin,
destination: destination,
waypoints : waypoints,
provideRouteAlternatives:false,
travelMode: google.maps.TravelMode.WALKING,
unitSystem: google.maps.UnitSystem.METRIC
}
directionsService.route(request, function(result, status) {
if(status == google.maps.DirectionsStatus.OK) {
//Cashe the results to render directions//
resultSet.push(result);
if(resultSet.length==iteration){
for(var i=0; i<iteration; i++){
directionsDisplayList[i].setDirections(resultSet[i]);
}
map.fitBounds(bounds);
}
}
});
}while (partialEndIndex <= maxmimumIndex);
}