Javascript Google Maps V3处理多个异步DirectionService.route请求
Google Maps V3 api不允许请求超过23个航路点。因此,我使用多个Javascript Google Maps V3处理多个异步DirectionService.route请求,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,Google Maps V3 api不允许请求超过23个航路点。因此,我使用多个directionservice.route功能划分航路点并将其分部分发送 function displayRoute(currentloc, waypoints, optimize, info) { var wypts = []; for (var i = 0; i < waypoints.length; i++) { wypts
directionservice.route
功能划分航路点并将其分部分发送
function displayRoute(currentloc, waypoints, optimize, info) {
var wypts = [];
for (var i = 0; i < waypoints.length; i++) {
wypts.push({
location: new google.maps.LatLng(waypoints[i].lat, waypoints[i].lng),
stopover: true
});
}
//Count of route request
lc = Math.ceil(wypts.length / 23);
//Array of requests
var mltwypts = [];
var i, temparray;
for (i = 0; i < lc; i++) {
if (i === 0) {
temparray = wypts.slice(i * 23, (i + 1) * 23);
mltwypts.push({
start: currentloc,
wys: temparray.slice(0, temparray.length - 1),
end: temparray[temparray.length - 1].location
});
} else {
temparray = wypts.slice(i * 23 - 1, (i + 1) * 23);
mltwypts.push({
start: temparray[0].location,
wys: temparray.slice(1, temparray.length - 1),
end: temparray[temparray.length - 1].location
});
}
}
dds = [];//DirectionsRenderers array
if (mltwypts.length > 0) {
for (var k = 0; k < mltwypts.length; k++) {
directionsService.route({
origin: mltwypts[k].start,
destination: mltwypts[k].end,
waypoints: mltwypts[k].wys,
optimizeWaypoints: optimize,
travelMode: google.maps.DirectionsTravelMode.DRIVING
}, function (response, status) {
if (status === 'OK') {
var directionsDisplay = new google.maps.DirectionsRenderer({preserveViewport: true});
directionsDisplay.setMap(map);
directionsDisplay.setDirections(response);
dds.push(directionsDisplay);
/*WHICH RESPONSE IS THIS?*/
} else {
window.alert('Directions request failed due to ' + status);
}
});
}
} else {
$("#ttdist").html("");
$("#tttime").html("");
$("#routeDetail").html("");
}
}
功能显示路线(当前位置、航路点、优化、信息){
var wypts=[];
对于(变量i=0;i0){
对于(var k=0;k
正如您在代码中看到的,
directionsService.route
函数返回一个回调函数。但我怎么知道他们要求的是哪种回应呢?那么如何在回调函数中传递k
值呢?谢谢 您可以这样做:
for (var k = 0; k < mltwypts.length; k++) {
function inner(index){
directionsService.route({
origin: mltwypts[k].start,
destination: mltwypts[k].end,
waypoints: mltwypts[k].wys,
optimizeWaypoints: optimize,
travelMode: google.maps.DirectionsTravelMode.DRIVING
}, function (response, status) {
if (status === 'OK') {
var directionsDisplay = new google.maps.DirectionsRenderer({preserveViewport: true});
directionsDisplay.setMap(map);
directionsDisplay.setDirections(response);
dds.push(directionsDisplay);
/*WHICH RESPONSE IS THIS?*/
// you can access the index here to determine which one it is.
} else {
window.alert('Directions request failed due to ' + status);
}
});
}
inner(k);
}
mltwypts.forEach(function(value, index){
directionsService.route({
origin: value.start,
destination: value.end,
waypoints: value.wys,
optimizeWaypoints: optimize,
travelMode: google.maps.DirectionsTravelMode.DRIVING
}, function (response, status) {
if (status === 'OK') {
var directionsDisplay = new google.maps.DirectionsRenderer({preserveViewport: true});
directionsDisplay.setMap(map);
directionsDisplay.setDirections(response);
dds.push(directionsDisplay);
// You can use `value` or `index` to determine which one it is.
} else {
window.alert('Directions request failed due to ' + status);
}
});
});