2013-06-04 135 views
-1

我有要求退出地理編碼JSON得到街道地址,城市,國家,郵編

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

地理編碼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) 

回答

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

錯誤嘗試獲取非對象的屬性$ address = $ resultObject-> results [0] - > address_components; echo $ long_name = $ address-> long_name; – pinky

+0

嘗試address_components [0];而不是address_components – harish

相關問題