1

I have the longitude and latitude into two separate EditText I want that when I press a button the street name appears in another EditText.

I tried with the Public Address getAddressForLocation method, but I have not gotten it to work..

Code

public Address getAddressForLocation(Context context, Location location) throws IOException {

        if (location == null) {
            return null;
        }
        double latitude = location.getLatitude();
        double longitude = location.getLongitude();
        int maxResults = 1;

        Geocoder gc = new Geocoder(context, Locale.getDefault());
        List<Address> addresses = gc.getFromLocation(latitude, longitude, maxResults);

        for (int i = 0; i < addresses.getMaxAddressLineIndex(); i++) {
            Log.d("=Adress=",addresses.getAddressLine(i));
        }
    }

How to get Street name from coordinates?

UPDATE (SOLUTION)

Geocoder geocoder = new Geocoder(this, Locale.getDefault());

            try {
                List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);

                if (addresses != null) {
                    Address returnedAddress = addresses.get(0);
                    StringBuilder strReturnedAddress = new StringBuilder();
                    for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) {
                        strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("");
                    }
                    et_lugar.setText(strReturnedAddress.toString());
                }
                else {
                    et_lugar.setText("No Address returned!");
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                et_lugar.setText("Canont get Address!");
            }

Thanks

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Aspicas
  • 4,498
  • 4
  • 30
  • 53

3 Answers3

4

The docs mention the getThoroughfare() method, which may be null. I'd try that first. If null, then I'd try to get something useful from getAddressLine(). It's possible you cannot get a street name for all cases.

JASON G PETERSON
  • 2,193
  • 1
  • 18
  • 19
3

SOLUTION

Geocoder geocoder = new Geocoder(this, Locale.getDefault());

                try {
                    List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);

                    if (addresses != null) {
                        Address returnedAddress = addresses.get(0);
                        StringBuilder strReturnedAddress = new StringBuilder();
                        for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) {
                            strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("");
                        }
                        et_lugar.setText(strReturnedAddress.toString());
                    }
                    else {
                        et_lugar.setText("No Address returned!");
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    et_lugar.setText("Canont get Address!");
                }
Aspicas
  • 4,498
  • 4
  • 30
  • 53
  • Hey, how i can know just the address ? how to know the index of address? it's variable :/ can help me? – Helio Soares Junior Mar 31 '16 at 16:51
  • @HelioSoaresJunior hey, you can use `addresses.get(0).getAddressLine(0)` and save it on `string`, when `addresses`it's a ` List
    addresses;`
    – Aspicas Apr 01 '16 at 11:33
  • Every android device of any manufacturer provides Geocoder.Can anyone provide some info on this context? Because recently used Geocoder in my application to get an address from coordinate but it works like a charm with device name Asus Zenfone and with the device like MiA1 doesn't even provide the address result. – blackjack Oct 05 '17 at 11:24
0

first of all, does your phone support location? you need to post the error as well so we know whats the real problem

assuming your phone supports locations, try using the getAdressLine() :

    List<Address> addresses = gc.getFromLocation(latitude, longitude, 1);
    Log.d("=Adress=",addresses.get(0).getAddressLine(0));

the catch here is getting what you want from the list that has the address class structure

mrahmat
  • 617
  • 6
  • 21