Search code examples
javaandroidgoogle-geocoder

Geocoder constructor cannot be defined


Hopefully this is a simple one guys. Im trying to create a geocoder object instance. I am getting an error that the constructor for the class is undefined. I don't understand why as I am supplying a context and default.ENGLISH.

I have the import and I have tried the context as both simply 'this' and 'context.this'.

import com.google.android.maps.GeoPoint;

This is the code in my method:

    public String convertGpToLoc(double lat, double longg)
    {

        Geocoder gc = new Geocoder(CityClickListener.this, Locale.US);

        List<Address> addresses = gc.getFromLocation(lat, longg, 1);
        StringBuilder sb = new StringBuilder();
        if (addresses.size() > 0) {
        Address address = addresses.get(0);
        for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
        sb.append(address.getAddressLine(i)).append("\n");
        sb.append(address.getLocality()).append("\n");
        sb.append(address.getPostalCode()).append("\n");
        sb.append(address.getCountryName());

        return sb.toString();

        }

        return null;

    }
}

Solution

  • The type of the first argument should be Context

    So based on your code, try he following:

    Geocoder gc = new Geocoder(ctx, Locale.US);