Я пытаюсь получить информацию о местоположении (название страны, название города и т. Д.), Имея долготу и широту. Но следующий код не возвращает никаких адресов, адрес.size() всегда равен нулю! Мне нужна помощь. Спасибо.Geocoder.getFromLocation() не возвращает адреса
public class FirstUsage extends Activity{
private LocationListener myLocListener;
private LocationManager myLocManager;
private TextView tv;
private Button okButt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_use_page);
tv = (TextView)findViewById(R.id.TextView_firstPageCoordination);
okButt = (Button)findViewById(R.id.firstPageOKButt);
myLocManager = (LocationManager) getSystemService(LOCATION_SERVICE);
setLocationChangeListener();
}
private void setLocationChangeListener() {
myLocListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
/*----------to get City-Name from coordinates ------------- */
String countryName = null;
Geocoder gcd = new Geocoder(FirstUsage.this, Locale.getDefault());
List<Address> addresses;
try {
addresses = gcd.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
if (addresses.size() > 0) {
Address returnedAddress = addresses.get(0);
countryName = returnedAddress.getCountryName();
}
} catch (IOException e) {
e.printStackTrace();
}
tv.setText("Your current country is: " + countryName + "\n Your current coordination is:\n" + "Longitude: " + location.getLongitude() + " Latitude: " + location.getLatitude());
/////
if (ActivityCompat.checkSelfPermission(FirstUsage.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(FirstUsage.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
myLocManager.removeUpdates(myLocListener);
}
Затем, что делать, если gcd.isPresent() не существует? –