I'm trying to use the Google Maps API to return the journey time and length from one point to another. Here is my code.
var routeInfoObject;
function findRoutes(given_origin, given_destination){
  var directionsService = new google.maps.DirectionsService();
  var directionsRequest = {
    origin: given_origin,
    destination: given_destination,
    travelMode: google.maps.DirectionsTravelMode.WALKING,
    unitSystem: google.maps.UnitSystem.METRIC,
    provideRouteAlternatives: true
  };
  var times = new Array();
  var lengths = new Array();
  var positions = new Array();
  directionsService.route(directionsRequest, function(response, status) {
      if (status == google.maps.DirectionsStatus.OK) {
         for (i = 0; i < response.routes.length && i < 4 ; i++) 
         {
             times[i] = response.routes[i].legs[0].duration.text;
             lengths[i] = response.routes[i].legs[0].distance.text;
         }
         routeInfoObject = times;
      } else { 
         console.log("There was an error with the request") 
      }
  });
  console.log(routeInfoObject);
}
The log at the bottom always returns undefined, even though routeInfoObject is a global variable and should be set to the value of times. Logging the value of times inside the callback function if I run findRoutes("longsight","rusholme") returns [18,18,18].
 
    