2012-05-23 126 views

回答

0

你知道如何顯示在你的Android應用的地圖?如果沒有,你可以看看this不錯的教程。

然後顯示的地址可以這樣做:

Geocoder geocoder = new Geocoder(this, Locale.getDefault()); 
    List<Address> addresses = geocoder.getFromLocation(lat, lng, 1); 
0

用於獲取點擊的座標,檢查此線程:

Getting coordinates when clicking anywhere on a MapView

,你可以把座標爲ADRESS與此:

Address a = ((Address)geocoder.getFromLocation(latiAsDouble, longiAsDouble, 1).get(0)); 
String Position = a.getAddressLine(1) + " - " + a.getAddressLine(0); 

這會做一個http c所有到谷歌服務器,所以你可能想把它放入某種線程。

6

我發現谷歌API是反向地理編碼非常方便。爲此我創建了這個類。您可以直接使用這個類在你的應用程序:

public class getReverseGeoCoding 
{ 
    private String Address1 = "", Address2 = "", City = "", State = "", Country = "", County = "", PIN = ""; 

    public void getAddress() 
    { 
     Address1 = ""; Address2 = ""; City = ""; State = ""; Country = ""; County = ""; PIN = ""; 

     try { 

      JSONObject jsonObj = parser_Json.getJSONfromURL("http://maps.googleapis.com/maps/api/geocode/json?latlng="+ Global.curLatitude + "," + Global.curLongitude +"&sensor=true"); 
      String Status = jsonObj.getString("status"); 
      if(Status.equalsIgnoreCase("OK")) 
      { 
       JSONArray Results = jsonObj.getJSONArray("results"); 
       JSONObject zero = Results.getJSONObject(0); 
       JSONArray address_components = zero.getJSONArray("address_components"); 

       for(int i = 0; i<address_components.length(); i++) 
       { 
        JSONObject zero2 = address_components.getJSONObject(i); 
        String long_name = zero2.getString("long_name"); 
        JSONArray mtypes = zero2.getJSONArray("types"); 
        String Type = mtypes.getString(0); 

        if(TextUtils.isEmpty(long_name) == false || !long_name.equals(null) || long_name.length() > 0 || long_name != "") 
        { 
         if(Type.equalsIgnoreCase("street_number")) 
         { 
          Address1 = long_name + " "; 
         } 
         else if(Type.equalsIgnoreCase("route")) 
         { 
          Address1 = Address1 + long_name; 
         } 
         else if(Type.equalsIgnoreCase("sublocality")) 
         { 
          Address2 = long_name; 
         } 
         else if(Type.equalsIgnoreCase("locality")) 
         { 
//       Address2 = Address2 + long_name + ", "; 
          City = long_name; 
         } 
         else if(Type.equalsIgnoreCase("administrative_area_level_2")) 
         { 
          County = long_name; 
         } 
         else if(Type.equalsIgnoreCase("administrative_area_level_1")) 
         { 
          State = long_name; 
         } 
         else if(Type.equalsIgnoreCase("country")) 
         { 
          Country = long_name; 
         } 
         else if(Type.equalsIgnoreCase("postal_code")) 
         { 
          PIN = long_name; 
         } 
        } 

//     JSONArray mtypes = zero2.getJSONArray("types"); 
//     String Type = mtypes.getString(0); 
//     Log.e(Type,long_name); 
       } 
      } 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 


    } 

    public String getAddress1() 
    { 
     return Address1; 

    } 

    public String getAddress2() 
    { 
     return Address2; 

    } 

    public String getCity() 
    { 
     return City; 

    } 

    public String getState() 
    { 
     return State; 

    } 

    public String getCountry() 
    { 
     return Country; 

    } 

    public String getCounty() 
    { 
     return County; 

    } 

    public String getPIN() 
    { 
     return PIN; 

    }