2016-08-25 86 views
-1

我創建一個web應用程序,我可以使用谷歌地圖API獲得距離和運行時間..解析JSONArray jQuery中

https://maps.googleapis.com/maps/api/distancematrix/json?origins=10.964,76.007&destinations=10.982,75.999 

我使用的HTTP方法,通過谷歌.. 其返回JSON作爲給定格式下面給出

{ 
    "destination_addresses" : [ "Kanyakumari - Panvel Highway, Palathara, Kerala 676501, India" ], 
    "origin_addresses" : [ "Kanyakumari - Panvel Highway, Randathani, Kerala 676510, India" ], 
    "rows" : [ 
     { 
     "elements" : [ 
      { 
       "distance" : { 
        "text" : "2.5 km", 
        "value" : 2526 
       }, 
       "duration" : { 
        "text" : "4 mins", 
        "value" : 228 
       }, 
       "status" : "OK" 
      } 
     ] 
     } 
    ], 
    "status" : "OK" 
} 

我怎麼能使用jQuery提取文本的距離和持續時間文本和提醒吧..

+1

你有什麼已經嘗試過? – haxtbh

+0

使用'jQuery.parseJSON()'。 – mnme

回答

1

使用file_get_contents()json_decode()輸出..

$contents = file_get_contents('https://maps.googleapis.com/maps/api/distancematrix/json?origins=10.964,76.007&destinations=10.982,75.999'); 
$data = json_decode($contents,true); 
echo 'Distance : '.$data['rows'][0]['elements'][0]['distance']['text']; 
echo "<br>"; 
echo 'Duration : '.$data['rows'][0]['elements'][0]['duration']['text']; 

這會給你:

Distance : 2.5 km 
Duration : 4 mins 
+1

是的,它的工作..謝謝兄弟.. –

+0

歡迎兄弟@KarthikCP很高興幫助你:)快樂編碼 –

+1

我可以轉換時間秒到這裏.. –