duanli0687 2015-01-31 09:39
浏览 84
已采纳

使用Google Maps Api(JSON)获取两个位置之间的距离

I want to get the distance between two locations using the Google API however this is the first time I hear about JSON and actually see its format. The variable $json_a should decode the JSON but when I try to get the distance I get "Notice: Undefined index: legs in C:\xampp\htdocs\app2\test.php on line 24". I added a link so you can see how the JSON structure looks like. I'll also want these locations ordered by distance and if you can provide any tips about how I could do that it would be great. Thanks! :)

PS: I know MySQL is depreciated, I'll look into MySQLi or PDO ASAP.

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

http://maps.googleapis.com/maps/api/directions/json?origin=Comanesti+Dimitrie+Ghica+13&destination=Brasov+1+Mai&alternatives=true&sensor=true

print_r shows me this:

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 ) 
  • 写回答

1条回答 默认 最新

  • dongpo5264 2015-01-31 09:49
    关注

    For finding the first value of routes, you can use below code :-

    $json_a['routes'][0]['legs'][0]['distance']['text']
    

    If you want to show all values then use foreach.

    For sorting on values

    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
    ";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于知识的学以致用的解释
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败