Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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!

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??

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top