2

my code :

                 try
                {

                            Geocoder geocoder;
                        List<Address> addresses;

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

            addresses = geocoder.getFromLocation(longitude,latitude,1);

            Toast.makeText(LocationActivity.this, addresses.size() + "", Toast.LENGTH_LONG).show();

            String address = addresses.get(0).getAddressLine(0);
            String city = addresses.get(0).getAddressLine(0);
            String country = addresses.get(0).getAddressLine(0);

            Toast.makeText(LocationActivity.this, address + "  " + city + " " + country, Toast.LENGTH_LONG).show();

        }
        catch(Exception e)
        {
            Toast.makeText(LocationActivity.this,e.toString(), Toast.LENGTH_LONG).show();
        }

this is my code to get address from longitude and latitude. but every time addresses.size returns 0

can anyone help me for this

piyush
  • 69
  • 1
  • 2
  • 8

5 Answers5

2

I find the solution... hold Ctrl on getFromLocation() and go to Geocoder.class . then download sources for your android API and restart your project. then you see its work

Pablo Barrera
  • 10,387
  • 3
  • 28
  • 49
0

if you are using emulator go to DDMS you will find emulator control and give your valid lat lng in that and press send.. and Batter make sure that mention Internet permissions in manifest...

    try {
        Geocoder geocoder;
        List<Address> addresses;

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

        double longitude = 23.00000;
        double latitude = 72.00000;
        addresses = geocoder.getFromLocation(longitude, latitude, 1);

        Toast.makeText(this, addresses.size() + "", Toast.LENGTH_LONG)
                .show();

        String address = addresses.get(0).getAddressLine(0);
        String city = addresses.get(0).getAddressLine(1);
        String country = addresses.get(0).getAddressLine(2);

        if(address==null)
        address="";
        if(city==null)
            city="";
        if(country==null)
            country="";

        Toast.makeText(this, address + "  " + city + " " + country,
                Toast.LENGTH_LONG).show();

    } catch (Exception e) {
        Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
    }
Sandeep P
  • 4,291
  • 2
  • 26
  • 45
0

Im not sure if this will help, but I think you are getting an IOException. Look at the LogCat. Also see this

light365
  • 369
  • 1
  • 4
  • 7
0

The Geocoder query methods will return an empty list if there no backend service in the platform.

ref - http://developer.android.com/reference/android/location/Geocoder.html

0

Fixed it with a condtion address.size() > 0 in the try loop.

I didn't really found out where the real problem is , but it seems that the loop is happening twice, and once too early , because i could have the loop happened twice , one empty result and one good result.

Maybe coming from me as as two map and date passing through intent. But fixed for me