【问题标题】:Google direction API not returning traffic parameter谷歌方向 API 不返回流量参数
【发布时间】:2015-12-28 08:52:06
【问题描述】:

我需要两个地方之间的距离,还需要使用谷歌方向 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)。

【问题讨论】:

    标签: php google-maps-api-3


    【解决方案1】:

    您必须在请求中指定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>
    

    【讨论】:

    • 有什么方法可以在同一个呼叫的单个响应中获取 BEST_GUESS、PESSIMISTIC 和 OPTIMISTIC 的 duration_in_traffic?
    【解决方案2】:

    要从 google api 获取 duration_in_traffic,您需要使用交通模式指定离开时间。

    $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; 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-15
      • 1970-01-01
      • 1970-01-01
      • 2015-04-03
      • 1970-01-01
      • 2012-07-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多