2014-10-01 81 views
0

Iam從我的代碼地理編碼中獲取地區名稱。如何從地理編碼中獲取地區名稱列表

$geocode=file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?latlng='.$latitude.','.$longitude.'&sensor=false'); 

我可以在這裏給的緯度列表和長而不是一個緯度和長,因爲我有一個700分,地理編碼花費過多的時間來計算。

回答

0

這很容易我的朋友。首先閱讀你的網址的迴應。 ......它會回報你的JSON然後使用下面的代碼運行

public static String getAddress(JSONObject result) { 
    if (result.has("results")) { 
     try { 
      JSONArray array = result.getJSONArray("results"); 
      if (array.length() > 0) { 
       JSONObject place = array.getJSONObject(0); 
       JSONArray components = place.getJSONArray("address_components"); 
       for (int i = 0; i < components.length(); i++) { 
        JSONObject component = components.getJSONObject(i); 
        JSONArray types = component.getJSONArray("types"); 
        for (int j = 0; j < types.length(); j++) { 
         if (types.getString(j).equals("route")) { 
          return component.getString("long_name"); 
         } 
        } 
       } 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 

    return null; 
} 

在我當前的代碼,我解析它的具體途徑....只是跳過條件和結果保存在您的數據結構對象

+0

但我如何給拉特lng列表獲取地址數組可以請告訴我或給我的線。 – 2014-10-02 05:28:31