2012-12-18 53 views
0

我的代碼是如何從PHP中使用file_get_contents()函數獲取的內容檢索特定數據?

$result = file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?address=".$stateName.",".$districtName.",".$cityName."&sensor=true"); 

其中$stateName$districtName$cityName是由用戶給出的值
進入喀拉拉邦艾娜庫蘭安加馬爾伊作爲Statename的districtName後和cityName我得到了這樣的結果:

{ 
    "results": [ 
     { 
      "address_components": [ 
       { 
        "long_name": "Angamaly", 
        "short_name": "Angamaly", 
        "types": [ 
         "locality", 
         "political" 
        ] 
       }, 
       { 
        "long_name": "Ernakulam", 
        "short_name": "Ernakulam", 
        "types": [ 
         "administrative_area_level_2", 
         "political" 
        ] 
       }, 
       { 
        "long_name": "Kerala State", 
        "short_name": "Kerala State", 
        "types": [ 
         "administrative_area_level_1", 
         "political" 
        ] 
       }, 
       { 
        "long_name": "India", 
        "short_name": "IN", 
        "types": [ 
         "country", 
         "political" 
        ] 
       } 
      ], 
      "formatted_address": "Angamaly, Kerala State, India", 
      "geometry": { 
       "bounds": { 
        "northeast": { 
         "lat": 10.2464704, 
         "lng": 76.39544959999999 
        }, 
        "southwest": { 
         "lat": 10.1585335, 
         "lng": 76.3500451 
        } 
       }, 
       "location": { 
        "lat": 10.212894, 
        "lng": 76.380601 
       }, 
       "location_type": "APPROXIMATE", 
       "viewport": { 
        "northeast": { 
         "lat": 10.2464704, 
         "lng": 76.39544959999999 
        }, 
        "southwest": { 
         "lat": 10.1585335, 
         "lng": 76.3500451 
        } 
       } 
      }, 
      "types": [ 
       "locality", 
       "political" 
      ] 
     } 
    ], 
    "status": "OK" 
} 

我需要該地點的經度和緯度。如何從
幾何性質 - > bounds->東北檢索LATLNG

+1

請看我的答案,測試和工作。 –

+0

已解決? –

+0

謝謝........... :) – raksuj

回答

2
$json = file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?address=".$stateName.",".$districtName.",".$cityName."&sensor=true"); 

$response = json_decode($json); 
echo $response->results[0]->geometry->bounds->northeast->lat; 
echo $response->results[0]->geometry->bounds->northeast->lng; 
相關問題