2011-05-19 61 views
0
Geocoder g = new Geocoder(this, Locale.getDefault()); 
    java.util.List<android.location.Address> result = null; 

    // testing 

    try{ 
     result = g.getFromLocation(43.324722,21.903333, 1); 
     if (result.size() > 0) { 
      selectedCity = result.get(0).getLocality(); 
     }else 
      //no city found 
     }catch(Exception ex) 
     { 

     } 

我用上面的代碼來獲得在Android的城市價值,但它始終沒有顯示出城市發現的任何一個指導我可能是什麼問題呢?無法從lat,long值中獲取城市?

回答

1
public void getAddress(double lat, double lng) { 
    Geocoder geocoder = new Geocoder(this, Locale.getDefault()); 
    try { 
     List<Address> addresses = geocoder.getFromLocation(lat, lng, 1); 
     Address obj = addresses.get(0); 
     String add = obj.getAddressLine(0); 
     GUIStatics.currentAddress = obj.getSubAdminArea() + "," 
       + obj.getAdminArea(); 
     GUIStatics.latitude = obj.getLatitude(); 
     GUIStatics.longitude = obj.getLongitude(); 
     GUIStatics.currentCity= obj.getSubAdminArea(); 
     GUIStatics.currentState= obj.getAdminArea(); 
     add = add + "\n" + obj.getCountryName(); 
     add = add + "\n" + obj.getCountryCode(); 
     add = add + "\n" + obj.getAdminArea(); 
     add = add + "\n" + obj.getPostalCode(); 
     add = add + "\n" + obj.getSubAdminArea(); 
     add = add + "\n" + obj.getLocality(); 
     add = add + "\n" + obj.getSubThoroughfare(); 

     Log.v("IGA", "Address" + add); 
     // Toast.makeText(this, "Address=>" + add, 
     // Toast.LENGTH_SHORT).show(); 

     // TennisAppActivity.showDialog(add); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show(); 
    } 
} 

我使用這個功能讓所有的經度和緯度只是通過經度和緯度在這個函數然後你發現你的答案的相關信息。 我希望這對你很有幫助。

+0

這是什麼guistatics CUDü請解釋!您一次又一次發佈此答案,但您並未告訴我們這是什麼GUISTATIcs?這是從一個lib還是什麼? – 2013-06-03 06:07:09