I am able to successfully get lat/long and pass it to the geocoder to get an Address. However, I don't always get an address back. Seems like it takes a couple of attempts? I'm not sure why.
Is there a better way for me to obtain the address at this point?
public List<Address> getAddresses(){
Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
List<Address> addresses = new ArrayList();
try {
addresses = geocoder.getFromLocation(latitude, longitude, 1);
} catch (IOException e) {
e.printStackTrace();
}
return addresses;
}
I am calling this method here:
LocationListener onLocationChange=new LocationListener() {
public void onLocationChanged(Location loc) {
//sets and displays the lat/long when a location is provided
String latlong = "Lat: " + loc.getLatitude() + " Long: " + loc.getLongitude();
latitude = loc.getLatitude();
longitude = loc.getLongitude();
List<Address> addresses = getAddresses();
if(addresses.size() > 0 && addresses != null)
zipcode = addresses.get(0).getPostalCode();
Toast.makeText(getApplicationContext(), zipcode,Toast.LENGTH_SHORT).show();
}