Google方向API不返回流量参数

时间:2015-12-28 08:52:07

标签: php google-maps-api-3

我需要两个地方之间的距离,并且还需要使用Google方向API估算它们之间的行程时间。 我正在使用谷歌API的文档。 https://developers.google.com/maps/documentation/javascript/directions

我正在使用以下代码。

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

我没有在响应中收到 duration_in_traffic 数据( $ xml )。

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

要从google api获取duration_in_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;