geocoder json获取街道地址,城市,国家,邮政编码

时间:2013-06-04 09:23:08

标签: php json geocoding

我确实要求退出

street name, postcode, city, state, country , latitude, longitude 从我的地址..

Geocode API以JSON格式在我的网络浏览器中返回此内容 什么是最常用的格式(long_name或short_name)

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "City Road",
               "short_name" : "City Rd",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Darlington",
               "short_name" : "Darlington",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "New South Wales",
               "short_name" : "NSW",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Australia",
               "short_name" : "AU",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "2008",
               "short_name" : "2008",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "City Road, Darlington NSW 2008, Australia",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : -33.88775940,
                  "lng" : 151.19316880
               },
               "southwest" : {
                  "lat" : -33.8913630,
                  "lng" : 151.18843910
               }
            },
            "location" : {
               "lat" : -33.88924140,
               "lng" : 151.19051430
            },
            "location_type" : "GEOMETRIC_CENTER",
            "viewport" : {
               "northeast" : {
                  "lat" : -33.88775940,
                  "lng" : 151.19316880
               },
               "southwest" : {
                  "lat" : -33.8913630,
                  "lng" : 151.18843910
               }
            }
         },
         "types" : [ "route" ]
      }
   ],
   "status" : "OK"
}

这是我的代码

$address = urlencode($address);
$googleApi = 'http://maps.googleapis.com/maps/api/geocode/json?address=%s&sensor=false';
$json = file_get_contents(sprintf($googleApi, $address));
$resultObject = json_decode($json);
$address = $resultObject->results[0]->address_components;

// please help me in pulling other data

$location = $resultObject->results[0]->geometry->location;
$latitude = $location->lat;
$longitude = $location->lng;

// (latitude and longitude is working)

1 个答案:

答案 0 :(得分:0)

    $location = $resultObject->results[0]->address_components[0];
    $long_name  =  $location->long_name;// long_name not ong_name
    $short_name = $location->short_name;