On Jun 10, 4:06 am, Seth <[email protected]> wrote:
> How can I get the
> driving time or duration using the GoogleMaps api?

because the driving directions service is asynchronous, you will have
to modify your own function to become asynchronous.  The following
shows the difference between synchronous and asynchronous function
usage, where the drivingTime function is being used to determine if a
route takes more than one hour:

------ SYNCHRONOUS FUNCTION USAGE ------

function getDrivingTime(start, end) {
  ...
  ...
  return time;
}

var time = getDrivingTime(start, end);

if (time > 3600) {
  // more than one hour, try another route
}

------ ASYNCHRONOUS FUNCTION USAGE ------

function getDrivingTime(start, end, responseHandler) {
....
....
  directionsService.route(request, function(response, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      responseHandler(response.routes[0].legs[0].duration.value);
    } else {
      responseHandler();
    }
  });
}


getDrivingTime(start, end, function(time) {
  if (time > 3600) {
    // more than one hour, try another route
  }

});

------------------------------------------

-- 
You received this message because you are subscribed to the Google Groups 
"Google Maps JavaScript API v3" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-maps-js-api-v3?hl=en.

Reply via email to