user3670592 user3670592 - 2 months ago 7
Android Question

Get location name from fetched coordinates get the name

I have found this solution: here to get the location name but I don't know how to display it in my

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 " + ????? + " ");
}

}


The t is the
textView

Answer

You have the name address in the String _Location. Therefore, you could set the value of TextView in the try block.

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);
}

Further Infomation

    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);

You could use this code to get further information about the adress