简体   繁体   中英

Geocoder.getFromLocation() does not return any addresses

I am trying to get location information(Country name, city name, etc) by having longitude and latitude. But, the following code does not return any addresses, the addresses.size() is always zero!! I need help. Thank you.

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

It appends sometimes when the connectivity isn't available or when the backend isn't implemented. The best way to verify those two thing is to check if the geocoder is present using this:

gcd.isPresent ()

Returns true if the Geocoder methods getFromLocation and getFromLocationName are implemented. Lack of network connectivity may still cause these methods to return null or empty lists.

From the android documentation :

The Geocoder class requires a backend service that is not included in the core android framework.

The issue is Geocoder backend service is not available in your device/simulator. Your device/simulator needs Google API in order to works correctly with Grocoder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM