5

I am trying to retrieve my location address using Google API v2.

Here is what i do:

Geocoder geocoder = new Geocoder(this, Locale.getDefault());
            List<Address> addresses = null;
            String addressText = "";
            try {
                while (addresses==null){
                    addresses = geocoder.getFromLocation(userLocation.latitude,
                            userLocation.longitude, 1);
                }

                if (addresses != null && addresses.size() > 0) {
                    Address address = addresses.get(0);
                    addressText = String.format(
                            "%s, %s, %s",
                            address.getMaxAddressLineIndex() > 0 ? address
                                    .getAddressLine(0) : "", address
                                    .getLocality(), address.getCountryName());
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

I have read several posts and found that the getFromLocation() request does not always retrieve a result. So i tried looping that.

This does not crash but addressText is always "", any ideas why? INTERNET permissions have been given to my app. I don't use GPS.

userLocation is not null, so that is not the problem, I use that to add a marker to the map and it works fine. It seems to be a problem with the geocoder strictly.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Adrian Olar
  • 2,883
  • 4
  • 35
  • 63
  • Have you tried upping the number of results? e.g. `addresses = geocoder.getFromLocation(userLocation.latitude, userLocation.longitude, 10);` Technically that shouldn't matter, just 'interested'! – iaindownie Jul 31 '13 at 09:06
  • Nope, unfortunately that doesn't work. but thanks for your prompt reply:) – Adrian Olar Jul 31 '13 at 09:18

4 Answers4

5

You can get address by two method 1. Using Geocoder 2. Using Google API

You can get your solution from this link. It has both solution. Visit my answer for Google API solution.

Get the particular address using latitude and longitude

Community
  • 1
  • 1
Vipul Purohit
  • 9,807
  • 6
  • 53
  • 76
  • i have checked your answer, i have a question, what type of object is parser_Json? – Adrian Olar Jul 31 '13 at 09:15
  • its a JSON Parsing class. It returns JSON Object from the given url. – Vipul Purohit Jul 31 '13 at 09:19
  • No its basically a class through which you read and parse json string and convert it to JSONObject. I have updated my answer in that question and added json_parser class. You can get it from there. – Vipul Purohit Jul 31 '13 at 09:28
  • thanks, i tried your solution but still no address is shown, nothing null but "". maybe there is a problem with the way i pass the coordinates, i will look more into that. thanks! – Adrian Olar Jul 31 '13 at 09:41
1

I manged to solve this... my code was throwing an exception of service not available on getFromLocation().

Details can be found here: https://code.google.com/p/android/issues/detail?id=38009

I rebooted my device and it works fine now. Thanks for all your help!

Adrian Olar
  • 2,883
  • 4
  • 35
  • 63
-1

TRY Rebooting Device! My code was crashing on the geocoder.getFromLocation(lat, lng, 1); and causing (IOException e) However after trying many permissions, code changes etc, I simply turned on/off my samsung phone and it worked... I thought Linux kernal was supposed to be better than windows, ie not needing reboot??

nneenh
  • 1
-1

Guys double check you haven't swapped your lat and longs :D I kept asking for an address in the middle of the Atlantic ocean and it took me about 3 hours to realize.

Also with emulator GPS you need to launch Google Maps at least once I have found, to initialize the play services location stuff.

Daniel Wilson
  • 18,838
  • 12
  • 85
  • 135