Christian
Christian

Reputation: 942

Geocoder keeps returning true

My application using a GeoCoder keeps returning the string to true. here is the code I have.

public boolean address(){
      Geocoder geoCoder = 
            new Geocoder
            (getBaseContext(), Locale.getDefault());

            try {
                List<Address> addresses = geoCoder.getFromLocation(LocationManagerHelper.getLatitude()/1E6, LocationManagerHelper.getLongitude()/1E6, 1);

                String addes = "";
                if (addresses.size() > 0) 
                {
                    for (int i=0; i<addresses.get(0).getMaxAddressLineIndex(); 
                         i++)
                       addes += addresses.get(0).getAddressLine(i) + "\n";
                }
            }
            catch (IOException e) {                
                e.printStackTrace();
            }
            return true;


 }

and I call that into a text view.

add.append(""+ address()
            + '\n');

it works somewhat except for it doesn't show the address and instead shows true

can any one tell me why this is?

thanks.

Upvotes: 0

Views: 192

Answers (2)

NickLH
NickLH

Reputation: 2645

You need to return the address string by using the return keyword, and changing your method return type to a String:

public String address(){
    Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
    String result = "";

    try {
        List<Address> addresses = geoCoder.getFromLocation(LocationManagerHelper.getLatitude()/1E6, LocationManagerHelper.getLongitude()/1E6, 1);

        if (addresses.size() > 0) 
        {
            for (int i=0; i<addresses.get(0).getMaxAddressLineIndex(); i++)
               result+= addresses.get(0).getAddressLine(i) + "\n";
        }
    }
    catch (IOException e) {                
        e.printStackTrace();
    }

    return result;
 }

Upvotes: 2

Giulio Piancastelli
Giulio Piancastelli

Reputation: 15808

You need to initialize the result String variable outside the try block, then change the method's return type to String and indeed return the result String variable after having augmented it with the lines from the address.

public String address() {
    Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
    String result = "";
    try {
        List<Address> addresses =
            geoCoder.getFromLocation(LocationManagerHelper.getLatitude() / 1E6,
                                     LocationManagerHelper.getLongitude() / 1E6,
                                     1);
            if (addresses.size() > 0) {
                Address address = addresses.get(0);
                for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
                    result += address.getAddressLine(i) + "\n";
            }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return result;
}

But, really, this is basic Java programming, so before delving into a framework such as Android I would heartily suggest you learn the language first.

Upvotes: 1

Related Questions