Raghbendra Nayak Raghbendra Nayak - 20 days ago 11
PHP Question

Google direction API not returning traffic parameter

I need the distance between two places and also estimated traveling time between them using google direction API.
I am using the documentation of google API.
https://developers.google.com/maps/documentation/javascript/directions

I am using below code.

//Source address
$a = 'Los Angeles International Airport, 1 World Way, Los Angeles, CA 90045, United States';
//Destination address
$b = 'Beverly Wilshire, Beverly Hills (A Four Seasons Hotel), 9500 Wilshire Boulevard, Beverly Hills, CA 90212, United
States';
//Pass source and destination address in google map API
$url = 'https://maps.googleapis.com/maps/api/directions/xml?origin=' .urlencode($a) . '&destination=' .urlencode($b) . '&travelMode=google.maps.TravelMode.DRIVING&drivingOptions=departureTime:1451302219|trafficModel:google.maps.TrafficModel.BEST_GUESS&key=AIzaSyC7h7m5bRs-BZwk0XTXEQTB74dZujeLzZs';
$data = file_get_contents($url);
$xml = new SimpleXMLElement($data);
echo "<pre>";print_r($xml);echo "<br/>";


I am not recieving the duration_in_traffic data in response($xml).

Answer

You must specify the departure_time parameter in the request. I modified your request like what is shown below and manage to get the duration_in_traffic in the response

Request:

https://maps.googleapis.com/maps/api/directions/xml?origin=place_id:ChIJtU-yE9KwwoAR8a2LaVd7qHc&destination=place_id:ChIJz-22Hfm7woARmoLOmvYW4J0&departure_time=1451302219&travelMode=google.maps.TravelMode.DRIVING&drivingOptions=trafficModel:google.maps.TrafficModel.BEST_GUESS&key=API_KEY

Response(`duration_in_traffic portion):

<duration_in_traffic>
<value>1459</value>
<text>24 mins</text>
</duration_in_traffic>
Comments