2015-12-28 22 views
0

我需要兩個地使用谷歌API向它們之間也預估行車時間之間的距離。 我正在使用google API的文檔。 https://developers.google.com/maps/documentation/javascript/directions谷歌API的方向不返回交通參數

我使用下面的代碼。

//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/>"; 

我不會響應recieving的duration_in_traffic數據($ XML)。

回答

0

您必須指定在請求中departure_time參數。我修改你的要求像什麼如下所示和管理,以獲得duration_in_traffic在響應

請求:

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 

響應(`duration_in_traffic部分):

<duration_in_traffic> 
<value>1459</value> 
<text>24 mins</text> 
</duration_in_traffic> 
+0

有沒有什麼辦法讓BEST_GUESS,PESSIMISTIC和OPTIMISTIC的duration_in_traffic在同一個調用中的單個響應中? –

0

爲了獲得從duration_in_traffic谷歌API,你需要指定與traffic模式的departure_time。

$url = 'https://maps.googleapis.com/maps/api/directions/xml?origin='.urlencode($a).'&destination='.urlencode($a).'&sensor=false&departure_time=1479142020&mode=driving&traffic_model=pessimistic&key='.API_KEY;