Php Google direction API未返回流量参数
我需要两个地方之间的距离,还需要使用google direction API估计两个地方之间的旅行时间。 我正在使用谷歌API的文档。 我正在使用下面的代码Php Google direction API未返回流量参数,php,google-maps-api-3,Php,Google Maps Api 3,我需要两个地方之间的距离,还需要使用google direction API估计两个地方之间的旅行时间。 我正在使用谷歌API的文档。 我正在使用下面的代码 //Source address $a = 'Los Angeles International Airport, 1 World Way, Los Angeles, CA 90045, United States'; //Destination address $b = 'Beverly Wilshire, B
//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/>";
//源地址
$a=‘美国加利福尼亚州洛杉矶世界路1号洛杉矶国际机场90045’;
//目的地址
$b='美国加利福尼亚州贝弗利山庄威尔希尔大道9500号贝弗利山庄贝弗利山庄贝弗利山庄(四季酒店),邮编90212
国家;
//在google map API中传递源地址和目标地址
$url='1https://maps.googleapis.com/maps/api/directions/xml?origin=“.urlencode($a)。”&目的地='.urlencode($b)。'&travelMode=google.maps.travelMode.DRIVING&drivingOptions=出发时间:1451302219 |交通模型:google.maps.trafficModel.BEST_GUESS&key=AIzaSyC7h7m5bRs-BZwk0XTXEQTB74dZujeLzZs';
$data=文件内容($url);
$xml=新的simplexmlement($data);
回声“;打印(xml);回声“
”;
我没有收到响应($xml)中的持续时间数据 您必须在请求中指定
出发时间
参数。我修改了您的请求,如下所示,并设法在响应中获得流量中的持续时间
请求:
<duration_in_traffic>
<value>1459</value>
<text>24 mins</text>
</duration_in_traffic>
响应(`duration_in_通信部分):
1459
24分钟
要从google api获取流量持续时间,您需要使用流量模式指定出发时间
有没有办法在同一个呼叫的单个响应中获得最佳猜测、悲观和乐观的持续时间?
<duration_in_traffic>
<value>1459</value>
<text>24 mins</text>
</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;