7

I am trying to get location address from Geocoder, I am able to get latitude and longitude but it returning address with zero length.

Here is my code snippet

            double longi = location.getLongitude();
            double latt = location.getLatitude();

            Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.ENGLISH);

            String add="";
            try {


                List<Address> address = geocoder.getFromLocation(latt/1E6,longi/1E6, 1);


                System.out.println("in location listener "+address.size());


                int i=0;

                if(address.size()>0)
                {
                    for(i=0;i<address.get(0).getMaxAddressLineIndex();i++)
                    {
                        add += address.get(0).getAddressLine(i);
                    }


                    Toast toast  = new Toast(getApplicationContext());
                    toast.makeText(getApplicationContext(), add, Toast.LENGTH_LONG).show();
                }   

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

Please help me to get it correct. Thanks in advance!

Aniruddh Ambarkar
  • 1,030
  • 1
  • 11
  • 20

2 Answers2

5

The Geocoder query methods will return an empty list if there no backend service in the platform. Use the isPresent() method to determine whether a Geocoder implementation exists.

See the Geocoder documentation for more information.

user695022
  • 579
  • 5
  • 16
Vishesh Chandra
  • 6,951
  • 6
  • 35
  • 38
  • 1
    I've never had a pleasant experience with the Geocoder API. But this is your best bet. – JustinDanielson May 08 '12 at 07:21
  • 6
    I have tried `getFromLocationName()` with different addresses. For some address I am getting result but for some addresses I am getting an array of size **0**. – Nigam Patro Feb 10 '16 at 12:40
  • 4
    I am already checking the Geocoder.isPresent() and this static function is returning true. Yet I am getting empty address list – AndroidEngineX Jul 02 '17 at 11:20
0

I think your project isn't targeting the correct AVD. To use geolocation you have to make it point an avd implementing the google APIs. Once You changed it everything will work fine.

Installing the Google APIs Add-On

K_Anas
  • 31,226
  • 9
  • 68
  • 81