我正在搞乱下面的代码,我似乎无法让它正常工作...从我收集的所有代码工作得很好,禁止划分$ distance1变量。
<?php
$start = 'EC1V 0ES';
$end = 'EC4R 3TN';
$url = 'http://maps.googleapis.com/maps/api/directions/xml?origin='.$start.'&destination='.$end.'&sensor=false';
$xml = simplexml_load_file($url);
$distance = $xml->route->leg->distance->text;
$distance1 = $distance/1.609; // divided by 1.609 to convert KM into Miles
echo $distance;
echo '<br />';
echo $distance1;
?>
是否有任何人有任何想法可能会导致什么?我已经在线测试并查看了GoogleApis网址(http://maps.googleapis.com/maps/api/directions/xml?origin=ec1v+0es&destination=ec4r+3tn&sensor=false),看起来好像我的代码正在提取正确的值,它只是没有划分或什么......啊,我很困惑,有点一个菜鸟,所以请很好地请:)
答案 0 :(得分:1)
你应该将$ distance转换为float
$start = 'EC1V 0ES';
$end = 'EC4R 3TN';
$url = 'http://maps.googleapis.com/maps/api/directions/xml?origin='.$start.'&destination='.$end.'&sensor=false';
$xml = simplexml_load_file($url);
$distance = (float) $xml->route->leg->distance->text;
$distance1 = $distance/1.609; // divided by 1.609 to convert KM into Miles
echo $distance;
echo '<br />';
echo $distance1;
// 4.4<br />2.7346177750155
答案 1 :(得分:0)
<?php
$start = 'EC1V 0ES';
$end = 'EC4R 3TN';
$url = 'http://maps.googleapis.com/maps/api/directions/xml?origin='.$start.'&destination='.$end.'&sensor=false';
$xml = simplexml_load_file($url);
$distance = (float) $xml->route->leg->distance->text;
$distance1 = round($distance/1.609,2); // divided by 1.609 to convert KM into Miles
echo $distance;
echo '<br />';
echo $distance1;
?>
更新的代码在这里:)感谢您的所有帮助!以上似乎现在工作正常:)