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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With