Search code examples
javaandroidgoogle-mapsgoogle-geocoder

GeoCorder: Cannot pass LatLng in getFromLocation()


Now I'm trying to get a location's country name but I cannot pass LatLng in getFromLocation(). How can I fix this?

public void checkCountry(LatLng location) {
     Geocoder gcd = new Geocoder(this, Locale.getDefaut());
     List<Address> addresses = gcd.getFromLocation(location.latitude, location.logtitude, 1); //error here
     String country = addresses.get(0).getCountryName();

The error says

Unhandled Exception: java.IO.Exception

getFromLocation() cannot be applied to: latitude double location.latitude longtitude double location.longtitude

What am I wrong with this?


Solution

  • just handle the exception. either throw it or catch it.

    public void checkCountry(LatLng location) {
        Geocoder gcd = new Geocoder(this, Locale.getDefaut());
        List<Address> addresses=new ArrayList<>();
        try{
            addresses=  gcd.getFromLocation(location.latitude, location.longitude, 1); //error here
        }catch (Exception e){
            e.printStackTrace();
        }
        String country;
        if(addresses!=null)if(addresses.size()!=0) country= addresses.get(0).getCountryName();
    }