【问题标题】:How do I parse JSON output from Google Maps API v3 distance matrix to PHP?如何解析从 Google Maps API v3 距离矩阵到 PHP 的 JSON 输出?
【发布时间】:2012-04-20 01:38:06
【问题描述】:

我只想将 Google Maps API 距离矩阵的输出转换为 PHP 以供进一步处理。我不知道如何将距离值返回到变量中。我的数据是a JSON file from Google’s Maps API v3

{
   "destination_addresses" : [ "San Francisco, Californie, États-Unis", "Victoria, BC, Canada" ],
   "origin_addresses" : [ "Vancouver, BC, Canada", "Seattle, Washington, États-Unis" ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "1 703 km",
                  "value" : 1703343
               },
               "duration" : {
                  "text" : "3 jours 19 heures",
                  "value" : 326836
               },
               "status" : "OK"
            },
            {
               "distance" : {
                  "text" : "138 km",
                  "value" : 138267
               },
               "duration" : {
                  "text" : "6 heures 45 minutes",
                  "value" : 24278
               },
               "status" : "OK"
            }
         ]
      },
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "1 451 km",
                  "value" : 1451182
               },
               "duration" : {
                  "text" : "3 jours 4 heures",
                  "value" : 274967
               },
               "status" : "OK"
            },
            {
               "distance" : {
                  "text" : "146 km",
                  "value" : 146496
               },
               "duration" : {
                  "text" : "2 heures 52 minutes",
                  "value" : 10321
               },
               "status" : "OK"
            }
         ]
      }
   ],
   "status" : "OK"
}

我是 JavaScript 和 JSON 新手,PHP 经验很少。

【问题讨论】:

    标签: php json google-maps-api-3


    【解决方案1】:
    $url = 'http://maps.googleapis.com/maps/api/distancematrix/json?origins=Vancouver+BC|Seattle&destinations=San+Francisco|Victoria+BC&mode=bicycling&language=fr-FR&sensor=false';
    $json = file_get_contents($url); // get the data from Google Maps API
    $result = json_decode($json, true); // convert it from JSON to php array
    echo $result['rows'][0]['elements'][0]['distance']['text'];
    

    【讨论】:

      【解决方案2】:

      试试这个。确保在 URL 中使用 + 而不是空格来分隔位置:

      <?php
          $url = 'https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=Place+City&destinations=Place+City';
      
          $ch = curl_init();
      
          curl_setopt($ch, CURLOPT_URL, $url);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
          curl_setopt($ch, CURLOPT_HEADER, 0);
          $response = curl_exec($ch);
          if(curl_error($ch))
          {
              echo 'error:' . curl_error($c);
          }
          else{
              echo json_decode($response,true);
          }
      
          curl_close($ch);
          ?>
      

      【讨论】:

        【解决方案3】:
        $url = 'https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=Hyderabad+Telangana+India&destinations=Mumbai+Maharashtra+India';
        $json = file_get_contents($url); 
        $result = json_decode($json, true);
        $mi = $result['rows'][0]['elements'][0]['distance']['text'];
        $km = $mi*1.60934;
        echo round($km)." KM <br/>";
        echo $result['rows'][0]['elements'][0]['duration']['text'];
        

        【讨论】:

          猜你喜欢
          • 2012-03-27
          • 2017-12-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-04-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多