我想使用Google API获取两个位置之间的距离,但这是我第一次听到JSON并实际看到它的格式。变量$ json_a应该解码JSON,但是当我试图获得距离时,我得到“注意:未定义的索引:第24行的C:\ xampp \ htdocs \ app2 \ test.php中的腿”。我添加了一个链接,以便您可以看到JSON结构的外观。我还希望这些地点按距离排序,如果你能提供关于我如何做到这一点的任何提示,那就太棒了。谢谢! :)
PS:我知道MySQL已经过折旧,我会尽快研究MySQLi或PDO。
//$origin = $_COOKIE['origin'];
//$id=$_GET['id'];
$origin = "Bucuresti+Avrig+30";
$id = 3;
$query = ("SELECT * FROM locations WHERE category_id='$id'");
$result = mysql_query($query) or die (mysql_error());
while($row = mysql_fetch_array($result))
{
$address = $row['Judet'] . '+' . $row['Localitate'] . '+' . $row['Strada'] . '+' . $row['Numar'];
$address = str_replace(' ', '+', $address);
$fullurl = 'http://maps.googleapis.com/maps/api/directions/json?origin=' . $origin . '&destination=' . $address . '&alternatives=true&sensor=true';
$json_a = json_decode((file_get_contents($fullurl)),true);
echo $row['name'];
echo "<span class='badge'>" . $json_a['legs']['routes']['distance']['text'] . "</span></button>";
}
print_r告诉我这个:
Array ( [routes] => Array ( ) [status] => NOT_FOUND ) Array ( [routes] => Array ( [0] => Array ( [bounds] => Array ( [northeast] => Array ( [lat] => 44.4521487 [lng] => 26.1311305 ) [southwest] => Array ( [lat] => 44.4451446 [lng] => 26.1251047 ) ) [copyrights] => Map data ©2015 Google [legs] => Array ( [0] => Array ( [distance] => Array ( [text] => 1.2 km [value] => 1168 ) [duration] => Array ( [text] => 2 mins [value] => 137 ) [end_address] => Șoseaua Colentina 2, București, Romania [end_location] => Array ( [lat] => 44.4521487 [lng] => 26.1267169 ) [start_address] => Strada Avrig 30, București, Romania [start_location] => Array ( [lat] => 44.4451446 [lng] => 26.1311305 ) [steps] => Array ( [0] => Array ( [distance] => Array ( [text] => 0.4 km [value] => 397 ) [duration] => Array ( [text] => 1 min [value] => 42 ) [end_location] => Array ( [lat] => 44.4479077 [lng] => 26.1279682 ) [html_instructions] => Head northwest on Strada Avrig toward Aleea Avrig [polyline] => Array ( [points] => cugnGqvn~Ce@h@w@`AEDiArA_BnBm@r@QRsA~AeAlAaAjA ) [start_location] => Array ( [lat] => 44.4451446 [lng] => 26.1311305 ) [travel_mode] => DRIVING ) [1] => Array ( [distance] => Array ( [text] => 0.2 km [value] => 158 ) [duration] => Array ( [text] => 1 min [value] => 22 ) [end_location] => Array ( [lat] => 44.447097 [lng] => 26.1263385 ) [html_instructions] => Turn left onto Strada Chiristigiilor [maneuver] => turn-left [polyline] => Array ( [points] => mfhnGybn~CJTj@rAZv@Rh@f@nAPh@ ) [start_location] => Array ( [lat] => 44.4479077 [lng] => 26.1279682 ) [travel_mode] => DRIVING ) [2] => Array ( [distance] => Array ( [text] => 0.3 km [value] => 282 ) [duration] => Array ( [text] => 1 min [value] => 32 ) [end_location] => Array ( [lat] => 44.4494197 [lng] => 26.1251047 ) [html_instructions] => Turn right onto Șoseaua Mihai Bravu [maneuver] => turn-right [polyline] => Array ( [points] => kahnGsxm~CiA@mAX[FGBMDWHEBc@Pa@T]TUPEBgBfB ) [start_location] => Array ( [lat] => 44.447097 [lng] => 26.1263385 ) [travel_mode] => DRIVING ) [3] => Array ( [distance] => Array ( [text] => 0.3 km [value] => 331 ) [duration] => Array ( [text] => 1 min [value] => 41 ) [end_location] => Array ( [lat] => 44.4521487 [lng] => 26.1267169 ) [html_instructions] => Turn right onto Șoseaua Colentina/DN2/E60/E85
Destination will be on the right
[maneuver] => turn-right [polyline] => Array ( [points] => {ohnG{pm~CYE[IoBk@oFeDYWYQq@[e@[ ) [start_location] => Array ( [lat] => 44.4494197 [lng] => 26.1251047 ) [travel_mode] => DRIVING ) ) [via_waypoint] => Array ( ) ) ) [overview_polyline] => Array ( [points] => cugnGqvn~CaL|MgCxCv@hBn@`Bx@xBiA@mAXc@JoAd@{A`AgBfBYEkCu@iG}DkAm@e@[ ) [summary] => Strada Avrig and Șoseaua Colentina/DN2/E60/E85 [warnings] => Array ( ) [waypoint_order] => Array ( ) ) [1] => Array ( [bounds] => Array ( [northeast] => Array ( [lat] => 44.4521487 [lng] => 26.131243 ) [southwest] => Array ( [lat] => 44.4438465 [lng] => 26.1251047 ) ) [copyrights] => Map data ©2015 Google [legs] => Array ( [0] => Array ( [distance] => Array ( [text] => 1.3 km [value] => 1282 ) [duration] => Array ( [text] => 3 mins [value] => 153 ) [end_address] => Șoseaua Colentina 2, București, Romania [end_location] => Array ( [lat] => 44.4521487 [lng] => 26.1267169 ) [start_address] => Strada Avrig 30, București, Romania [start_location] => Array ( [lat] => 44.4451446 [lng] => 26.1311305 ) [steps] => Array ( [0] => Array ( [distance] => Array ( [text] => 9 m [value] => 9 ) [duration] => Array ( [text] => 1 min [value] => 1 ) [end_location] => Array ( [lat] => 44.4450833 [lng] => 26.1312017 ) [html_instructions] => Head southeast on Strada Avrig [polyline] => Array ( [points] => cugnGqvn~CJM ) [start_location] => Array ( [lat] => 44.4451446 [lng] => 26.1311305 ) [travel_mode] => DRIVING ) [1] => Array ( [distance] => Array ( [text] => 0.2 km [value] => 209 ) [duration] => Array ( [text] => 1 min [value] => 45 ) [end_location] => Array ( [lat] => 44.4439255 [lng] => 26.1293794 ) [html_instructions] => Take the 1st right toward Șoseaua Mihai Bravu [polyline] => Array ( [points] => wtgnG_wn~CHCFAFAFAD@F@D@D@DBFH~@jCHRRj@BHZ`AJVPn@ ) [start_location] => Array ( [lat] => 44.4450833 [lng] => 26.1312017 ) [travel_mode] => DRIVING ) [2] => Array ( [distance] => Array ( [text] => 0.7 km [value] => 733 ) [duration] => Array ( [text] => 1 min [value] => 66 ) [end_location] => Array ( [lat] => 44.4494197 [lng] => 26.1251047 ) [html_instructions] => Turn right onto Șoseaua Mihai Bravu [maneuver] => turn-right [polyline] => Array ( [points] => qmgnGskn~CNf@KPGL_@n@]j@m@|@e@h@[Za@b@eBrAqBxAIFk@Z_@R{@X{@RiA@mAX[FGBMDWHEBc@Pa@T]TUPEBgBfB ) [start_location] => Array ( [lat] => 44.4439255 [lng] => 26.1293794 ) [travel_mode] => DRIVING ) [3] => Array ( [distance] => Array ( [text] => 0.3 km [value] => 331 ) [duration] => Array ( [text] => 1 min [value] => 41 ) [end_location] => Array ( [lat] => 44.4521487 [lng] => 26.1267169 ) [html_instructions] => Turn right onto Șoseaua Colentina/DN2/E60/E85
Destination will be on the right
[maneuver] => turn-right [polyline] => Array ( [points] => {ohnG{pm~CYE[IoBk@oFeDYWYQq@[e@[ ) [start_location] => Array ( [lat] => 44.4494197 [lng] => 26.1251047 ) [travel_mode] => DRIVING ) ) [via_waypoint] => Array ( [0] => Array ( [location] => Array ( [lat] => 44.4445541 [lng] => 26.130791 ) [step_index] => 1 [step_interpolation] => 0.36662061075431 ) ) ) ) [overview_polyline] => Array ( [points] => cugnGqvn~CTQNCZBJDFH~@jC\~@^jA\fANf@KPg@|@kAhBaAdAa@b@eBrA{B`BkAn@wBl@iA@mAXc@Je@Ni@T_Aj@[TgBfBYEkCu@iG}DkAm@e@[ ) [summary] => Șoseaua Mihai Bravu and Șoseaua Colentina/DN2/E60/E85 [warnings] => Array ( ) [waypoint_order] => Array ( ) ) ) [status] => OK )
答案 0 :(得分:0)
要查找路线的第一个值,您可以使用以下代码: -
$json_a['routes'][0]['legs'][0]['distance']['text']
如果您想显示所有值,请使用foreach
。
用于对值进行排序
function cmp($a, $b)
{
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
$a = array(3, 2, 5, 6, 1);
usort($a, "cmp");
foreach ($a as $key => $value) {
echo "$key: $value\n";
}