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

时间:2011-04-02 20:07:49

标签: php json curl maps

我是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'];

}

所有这一切的目标是从结果中拉出lat和lng值 - >几何 - >以下JSON的位置数组:

{
  "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
        }
      }
    }
  } ]
}

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

很抱歉,如果这是一个简单的问题,但就像我说的那样,我是新手,现在让我发疯了。

非常感谢! :)

2 个答案:

答案 0 :(得分: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']);


我想,根据您搜索到的地址,您有时会在results数组中有多个项目。

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