1

I used reverse geocoding from Google Maps to get an address, and the first time it worked, put the address completely, but now it just shows me latitude and longitude, no name, in any address.

Can someone help me?

My code:

private class GetAddressTask extends AsyncTask<Location, Void, String> {

    @Override
    protected String doInBackground(Location... params) {
        Geocoder geocoder = new Geocoder(MainActivity.this);
        Location loc = params[0];
        List<Address> addresses = null;

        try {
            addresses = geocoder.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1);
        } catch (IOException e) {
            return null;
        }

        if( addresses != null && addresses.size() > 0 ) {
            Address addr = addresses.get(0);
            String addressText = addr.getAddressLine(0);
            updateLastStreet(addressText, MainActivity.this);
            return addressText;
        }

        return null;
    }
}
President James K. Polk
  • 40,516
  • 21
  • 95
  • 125

1 Answers1

1

Are you missing a permission in Android.Manifest file, then add this

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

And try this method:

public List<Address> getAddress(LatLng point) {
    try {
        Geocoder geocoder;
        List<Address> addresses;
        geocoder = new Geocoder(this);
        if (point.latitude != 0 || point.longitude != 0) {
            addresses = geocoder.getFromLocation(point.latitude ,
                    point.longitude, 1);
            String address = addresses.get(0).getAddressLine(0);
            String city = addresses.get(0).getAddressLine(1);
            String country = addresses.get(0).getAddressLine(2);
            return addresses;

        } else {
            Toast.makeText(this, "Coordinates are null",
                    Toast.LENGTH_SHORT).show();
            return null;
        }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
gorp88
  • 105
  • 14