使用PHP(curl)从JSON(Google Maps API)提取数据

Jac*_*k W 21 php maps json curl

我是JSON的新手,我正在尝试使用curl从Google Maps API获取地理编码城市的纬度和经度.我正在使用的功能是:

function geocode($city){
   $cityclean = str_replace (" ", "+", $city);
   $details_url = "http://maps.googleapis.com/maps/api/geocode/json?address=" . $cityclean . "&sensor=false";

   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $details_url);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   $geoloc = json_decode(curl_exec($ch), true);

   $step1 = $geoloc['results'];
   $step2 = $step1['geometry'];
   $coords = $step2['location'];

   print $coords['lat'];
   print $coords['lng'];

}
Run Code Online (Sandbox Code Playgroud)

所有这一切的目标是从以下JSON的Results - > Geometry - > Location数组中提取lat和lng值:

{
  "status": "OK",
  "results": [ {
    "types": [ "locality", "political" ],
    "formatted_address": "Westminster, London, UK",
    "address_components": [ {
      "long_name": "London",
      "short_name": "London",
      "types": [ "locality", "political" ]
    }, {
      "long_name": "Westminster",
      "short_name": "Westminster",
      "types": [ "administrative_area_level_3", "political" ]
    }, {
      "long_name": "Greater London",
      "short_name": "Greater London",
      "types": [ "administrative_area_level_2", "political" ]
    }, {
      "long_name": "England",
      "short_name": "England",
      "types": [ "administrative_area_level_1", "political" ]
    }, {
      "long_name": "United Kingdom",
      "short_name": "GB",
      "types": [ "country", "political" ]
    } ],
    "geometry": {
      "location": {
        "lat": 51.5001524,
        "lng": -0.1262362
      },
      "location_type": "APPROXIMATE",
      "viewport": {
        "southwest": {
          "lat": 51.3493528,
          "lng": -0.3783580
        },
        "northeast": {
          "lat": 51.7040647,
          "lng": 0.1502295
        }
      },
      "bounds": {
        "southwest": {
          "lat": 51.3493528,
          "lng": -0.3783580
        },
        "northeast": {
          "lat": 51.7040647,
          "lng": 0.1502295
        }
      }
    }
  } ]
}
Run Code Online (Sandbox Code Playgroud)

但是,它不会打印任何内容.我知道该函数成功从Google检索JSON并将其带到我的服务器,因为我为'status'值打了一个print语句,然后返回'OK'.问题似乎是当我尝试深入研究JSON时.

对不起,如果这是一个直截了当的问题,但就像我说的,我是新手,现在让我发疯了.

非常感谢!:)

Pas*_*TIN 19

请注意,它似乎results包含一个数组 (此处只有一个项目)的结果; 并且geometry是一个结果中的一个项目.

在这里,您可以看到结果的内容由[]- 分隔- 表示它是一个数组.


因此,您必须首先访问第一个结果:$geoloc['results'][0]
在其中您将拥有几何:$geoloc['results'][0]['geometry']

这将允许您获得经度和纬度:

var_dump($geoloc['results'][0]['geometry']['location']['lat']);
var_dump($geoloc['results'][0]['geometry']['location']['lng']);
Run Code Online (Sandbox Code Playgroud)


我想,根据您搜索过的地址,您有时会在results阵列中有多个项目.


小智 19

你只需要这个:

$fullurl = "http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true";
$string .= file_get_contents($fullurl); // get json content
$json_a = json_decode($string, true); //json decoder

echo $json_a['results'][0]['geometry']['location']['lat']; // get lat for json
echo $json_a['results'][0]['geometry']['location']['lng']; // get ing for json
Run Code Online (Sandbox Code Playgroud)

  • 这可能有用,但他问卷曲,而不是file_get_contents. (2认同)