2010-04-24 95 views
2

我想解析從谷歌地圖地理編碼版本3通過JSON數據。我想獲得詳細信息,如locaityName,AdministrativeAreaName和狀態碼。我可以知道如何解析這些數據嗎?謝謝解析數據從谷歌地圖API與PHP與JSON與PHP

+0

男人,2年後,仍然沒有人直接回答你的問題。 – Ray 2012-08-07 18:56:29

回答

0

PHP有json_decode(),將一個JSON數據流轉換爲PHP數組或對象。

+1

我想解析來自Google地理編碼的具體數據,正如我上面提到的。你知道解析這些數據的結構嗎?謝謝。 – davidlee 2010-04-24 10:10:50

+1

@ben你爲什麼不直接發出請求,並用'print_r()'找出自己?這是最簡單的方法。 – 2010-04-24 10:16:53

+0

我會試試看。謝謝! – davidlee 2010-04-24 17:02:58

3
function getGoogleAddressCoordinates() 
    { 
     //using the google maps api to get the long and lat coordinates of addresss 
     //using form post variables 
     $address =trim($_POST['address']); 
     $city = trim($_POST['city']); 
     $state = trim($_POST['state']); 

     //formats the address add '+' into space 
     $address = str_replace(' ', '+' ,$address); 
     $city = str_replace(' ', '+', $city); 
     $address = preg_replace("[^A-Za-z0-9]", '+', $address); //remove non alpha numeric chars such as extra spaces. 
     $address_str = $address.',+'. $city.',+'.$state; 
     $geo_url = "http://maps.google.com/maps/api/geocode/json?address=$address_str&sensor=false"; 
     //echo $geo_url; 
     //echo file_get_contents($geo_url); 
     $json_result = json_decode(file_get_contents($geo_url)); 

     $geo_result = $json_result->results[0]; 
     $coordinates = $geo_result->geometry->location; 

     //see if the it is locating the real address or just apox location, or in most cases a bad address 
     if($geo_result->types[0] == 'street_address') 
      $coordinates->valid_address = TRUE; 
     else 
      $coordinates->valid_address = FALSE; 

     return $coordinates; 
    }