在Jquery中解析JSONArray

时间:2016-08-25 09:52:48

标签: php jquery google-maps

我正在创建一个webapp,我可以使用谷歌地图API获取旅行的距离和时间..

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 个答案:

答案 0 :(得分: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