I want to get country name from user location so for this purpose i find latitude and longitude of current location. Now i want to get country name from this lat and longi. But problem is i am not getting country name, dont know why please check my code and help me.
HomeActivity.java:
if (checkPermissions()) {
if (isLocationEnabled()) {
mFusedLocationClient.getLastLocation().addOnCompleteListener(
new OnCompleteListener<Location>() {
@Override
public void onComplete(@NonNull Task<Location> task) {
Location location = task.getResult();
if (location == null) {
requestNewLocationData();
} else {
lat = location.getLatitude(); //here i am getting latitude and logitude value
lon = location.getLongitude();
}
}
}
);
} else {
Toast.makeText(this, "Turn on location", Toast.LENGTH_LONG).show();
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
} else {
requestPermissions();
}
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address>addresses;
try{
addresses = geocoder.getFromLocation(lat, lon, 10);
if(addresses.size()>0){
for(Address adr: addresses){
if(adr.getCountryName()!= null && adr.getCountryName().length()>0){
countryname = adr.getCountryName();
latlocation.setText(countryname); //but here country name is showing nothing.
break;
}
}
}
}catch (Exception e){
e.printStackTrace();
}
Please help me.