Google Distance Matrix Api: travel time with traffic

I am trying to get the travel time with traffic between two sets of latitude and longitude. I can call the Google Matrix API, but I only get the travel time without traffic . The API notes I read says it uses a parameter named checkout. Here's an excerpt:

"Departure Time" indicates the desired departure time in seconds since midnight January 1, 1970 GMT. Card for Business customers can enter a departure time to specify a departure time to obtain a trip time based on current traffic conditions. The departure time must be set to within a few minutes of the current time. "

I found this site to give me this time: epochconverter

However, I still get the same travel time. Here is an example request, the time_out parameter needs to be updated (not that it matters).

https://maps.googleapis.com/maps/api/distancematrix/xml?units=imperial&departure_time=1408046331&origins=37.407585,-122.145287&destinations=37.482890,-122.150235

15 minutes always returns.

When using "maps dot google dot com", the travel time of 19 minutes is returned when traffic is taken into account.

If someone can help me get the travel time with traffic from the Distance Matrix API it would be very appreciated.

+5


source to share


6 answers


You don't need a business license, you just need an API key from the project at https://console.developers.google.com/ with Google Distance Matrix enabled. For Google map results, use traffic_model with pessimistic, optimistic and keep in mind that "the departure time must be set within a few minutes of the current time", otherwise it will always return 15 minutes.



+5


source


This feature is only available to Maps for Business clients according to the docs .



+3


source


Even with a business license, you can only request the departure time after 5 minutes if you are using travel driving mode

https://developers.google.com/maps/documentation/distancematrix/

+2


source


Please use the below code.

var origin = new google.maps.LatLng(detectedLatitude,detectedLongitude);       
var destination = new google.maps.LatLng(latitudeVal,langtitudeVal);
var service = new google.maps.DistanceMatrixService();var date = new Date();
    date.setDate(date.getDate() + 1);
    var DrivingOptions = {
    departureTime: date,
    trafficModel: 'pessimistic'
    };

    service.getDistanceMatrix(
      {
        origins: [origin],
        destinations: [destination],
        travelMode: 'DRIVING',
        drivingOptions : DrivingOptions,
        unitSystem: google.maps.UnitSystem.METRIC,
        durationInTraffic: true,
        avoidHighways: false,
        avoidTolls: false
      }, response_data);function response_data(responseDis, status) {
  if (status !== google.maps.DistanceMatrixStatus.OK || status != "OK"){
    console.log('Error:', status);
    // OR
    alert(status);
  }else{
       alert(responseDis.rows[0].elements[0].distance.text);
  }});

      

Please refer to this document click here

+2


source


According to google docs, "departure_time" can only be used if "mode" (travel mode) is set to "Driving" (which is the standard travelMode) and Api KEY is included in your request. There is also an optional "trafficModel" parameter. Here is an example url with correct parameters. https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=40.6655101,-73.89188969999998&destinations=40.598566%2C-73.7527626&mode=driving&departure_time=nowEY&traffic_model=op

To use distance matrix api as javascript code use it as pointed out in this document. https://developers.google.com/maps/documentation/javascript/distancematrix

** Imp: ** There are many limitations with this API. Most of the features are only available to premium users. Please read the above document carefully.

+1


source


If you are using the golang client, set the DepartureTime to now in the DirectionsRequest input parameter of the Directions function.

0


source







All Articles