【问题标题】:geocoder json to get street address, city, country ,postcodegeocoder json 获取街道地址、城市、国家、邮政编码
【发布时间】:2013-06-04 09:23:07
【问题描述】:

我确实需要退出

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)

【问题讨论】:

    标签: php json geocoding


    【解决方案1】:
        $location = $resultObject->results[0]->address_components[0];
        $long_name  =  $location->long_name;// long_name not ong_name
        $short_name = $location->short_name;
    

    【讨论】:

    • 错误尝试获取非对象的属性 $address = $resultObject->results[0]->address_components; echo $long_name = $address->long_name;
    • 尝试 address_components[0];而不是 address_components
    猜你喜欢
    • 2012-09-15
    • 2018-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-27
    • 1970-01-01
    相关资源
    最近更新 更多