0

So I've tried many different solutions as found online and on StackOverflow but I just can't seem to get this to work. Here is what I am working with right now which I have from a similar post on SO. What am I doing wrong?

public void getLocationName() {

    String strAdd = "";
    Geocoder geocoder = new Geocoder(this, Locale.getDefault());
    try {
        List<Address> addresses = geocoder.getFromLocation(49.123124, -122.652404, 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("\n");
            }
            strAdd = strReturnedAddress.toString();
            Log.w("My Current loction address", "" + strReturnedAddress.toString());
        } else {
            Log.w("My Current loction address", "No Address returned!");
        }
    } catch (Exception e) {
        e.printStackTrace();
        Log.w("My Current loction address", "Cannot get Address!");
    }
    Toast.makeText(getApplicationContext(), "Distance between two locations is: " + strAdd.toString(), Toast.LENGTH_LONG).show();
}

It always throws the exception, so is it my coordinates?

Tudor Hofnar
  • 257
  • 1
  • 8
  • 20

2 Answers2

0

This is a working code I have, try it.

 public void GiveResult() {
            Geocoder myLocation = new Geocoder(getApplicationContext(),
                    Locale.getDefault());
            try {
                List<Address> myList = myLocation.getFromLocation(latitude,
                        longitude, 1);

                if (myList != null && myList.size() > 0) {
                    Address address = myList.get(0);
                    result = address.getLocality();

                }
                Iterator<Address> it = myList.iterator();
                while (it.hasNext()) {

                    TextView tv = (TextView) findViewById(R.id.tv);
                    tv.setText("Data is" + it.next());

                }

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
Skynet
  • 7,522
  • 5
  • 40
  • 78
  • Can't seem to get this working. For me the myList is empty, no elements in it...any idea why? I tried my lat long online and it is a an actual location. – Tudor Hofnar Jan 12 '14 at 21:02
0

try this,

Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
addresses = geocoder.getFromLocation(latitude, longitude, 1);
String address = addresses.get(0).getAddressLine(0); 
getMaxAddressLineIndex()
String city = addresses.get(0).getLocality();
String state = addresses.get(0).getAdminArea();
String country = addresses.get(0).getCountryName();
String postalCode = addresses.get(0).getPostalCode();
String knownName = addresses.get(0).getFeatureName();
man issar
  • 41
  • 1
  • 4