これが私のサンプルコードです
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);
この状況では、コンソールのselected_pathは[]です。console.log(selected_path)の前にalert(i)行を追加すると。追加後のコードは次のとおりです。
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);
変数iは画面にアラートのように表示され、変数selected_pathには適切な値があります。私はそれを理解していないので、誰かが私にそれを説明してもらえますか?明確にするために、Firefoxで機能しますが、おそらくChromeでは機能しません。