In the code below (watch error console!) I'm rapidly querying Google Maps for ten different locations to know wether it can calculate a route to there or not. This does work, but I need the result from Google to come in before continuing with the loop (alternating lines in the console, not like right now where my loop runs through and only then comes the status from Google).
How do I do this?
After trying this with callbacks for quite a while, I learned here Google Maps V3 setDirections() callback I probably need to do this with an event listener. So I tried searching the API Reference but could not find anything even close... but then again, I'm quite the newb so... any ideas?
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title></title>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?"></script>
</head>
<body style="font-family: Arial; font-size: 12px; color:#FFFFFF;" bgcolor="#202020">
<div id="map" style="width: 300px; height: 300px;"></div>
<script type="text/javascript">
var counter = 0;
var destination_lat = 52.498775;
var destination_long = 13.518474;
do {
var destination_long = (destination_long + 0.2);
var destination = destination_lat + ", " + destination_long;
var finaldestination = destination.toString();
calcRoute();
console.error('longitude: ' + destination_long.toFixed (1) + ', counter: ' + counter);
counter = (counter + 1);
}
while (counter < 10);
function calcRoute() {
var directionsService = new google.maps.DirectionsService();
var request = {
origin: 'Potsdamer Platz, 10785 Berlin',
destination: finaldestination,
travelMode: google.maps.DirectionsTravelMode.TRANSIT,
};
directionsService.route(request, function(response, status) {
console.error('DirectionsStatus is ' + status);
});
}
</script>
</body>
</html>