2016-09-29 166 views
0

我發現這個解決方案的名稱:here獲得位置的名字,但我不知道如何顯示它我textView獲取從獲取的座標位置名稱得到

LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
String provider = locationManager.getBestProvider(new Criteria(), true); 

Location locations = locationManager.getLastKnownLocation(provider); 
List<String> providerList = locationManager.getAllProviders(); 
if(null!=locations && null!=providerList && providerList.size()>0){     
double longitude = locations.getLongitude(); 
double latitude = locations.getLatitude(); 
Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());     
try { 
    List<Address> listAddresses = geocoder.getFromLocation(latitude, longitude, 1); 
    if(null!=listAddresses&&listAddresses.size()>0){ 
     String _Location = listAddresses.get(0).getAddressLine(0); 
    } 
} catch (IOException e) { 
    e.printStackTrace(); 
    t.append("\n " + ????? + " "); 
} 

} 

的t是textView

回答

2

您在字符串_Location中有姓名地址。因此,您可以在try塊中設置值TextView

try { 
List<Address> listAddresses = geocoder.getFromLocation(latitude, longitude, 1); 
if(null!=listAddresses&&listAddresses.size()>0){ 
    String _Location = listAddresses.get(0).getAddressLine(0); 
} 
t.setText(_Location); 
} 

進一步信息來源

String address1 = listAddresses.get(0).getAddressLine(0); 
    String city = listAddresses.get(0).getLocality(); 
    String state = listAddresses.get(0).getAdminArea(); 
    String country = listAddresses.get(0).getCountryName(); 
    String postalCode = listAddresses.get(0).getPostalCode(); 
    String knownName = listAddresses.get(0).getFeatureName(); 

    t.setText(address1 + " " + city + "\n" + state + " " + postalCode); 

你可以使用此代碼來獲取有關ADRESS

+0

這項工作就像魔術HAHAH更多信息,感謝的人很多 – user3670592