Here is my sample code
var selected_path = [];
for (var i = 0; i <path.length-1; i++) {
var request = {
origin: path[i],
destination: path[i+1],
travelMode: google.maps.DirectionsTravelMode.WALKING
}
directionsService.route(request, function(results, status) {
if (status == google.maps.DirectionsStatus.OK) {
selected_path = selected_path.concat(results.routes[0].overview_path);
}
})
}
console.log(selected_path); // slected_path in console is []
poly.setPath(selected_path);
poly.setMap(map);
In this situation selected_path in console is []. When I add alert(i) line before console.log(selected_path). Here is code after addition:
var selected_path = [];
for (var i = 0; i <path.length-1; i++) {
var request = {
origin: path[i],
destination: path[i+1],
travelMode: google.maps.DirectionsTravelMode.WALKING
}
directionsService.route(request, function(results, status) {
if (status == google.maps.DirectionsStatus.OK) {
selected_path = selected_path.concat(results.routes[0].overview_path);
}
})
}
alert(i) ////// ADDED ONE LINE
console.log(selected_path); /// selected_path in console has proper value
poly.setPath(selected_path);
poly.setMap(map);
Variable i is showing like an alert on screen and variable selected_path has proper value. Could somebody explain it to me, because I dont't get it. To clarify it works in firefox, probably not in chrome.
letand you uselet i = 0;in the loop header.