Reputation: 126
private String getCompleteAddressString(double LATITUDE, double LONGITUDE)
{
String strAdd = "";
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);
if (addresses != null)
{
Address returnedAddress = addresses.get(0);
StringBuilder strReturnedAddress = new StringBuilder("");
for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++)
{
strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
}
strAdd = strReturnedAddress.toString();
Log.w("My Current loction address", "" + strReturnedAddress.toString());
Toast.makeText(getApplicationContext(), "Address :" +strAdd, Toast.LENGTH_LONG).show();
}
else
{
Log.w("My Current loction address", "No Address returned!");
}
} catch (Exception e) {
e.printStackTrace();
Log.w("My Current loction address", "Canont get Address!");
}
return strAdd;
}
This is my code but it requires Internet Connection.. when i run this without internet connection it throws Exception server time-out.
Upvotes: 0
Views: 1156
Reputation: 4981
You can't get latitude and longitude offline without internet. You can get it by network, gps or location.getLastKnownLocation(LocationManager.NETWORK_PROVIDER)
(without internet).
See this https://stackoverflow.com/a/22085632/3864698. Maybe it can help you.
Upvotes: 1
Reputation: 21
Since i must not comment with my reputation, sorry for not providing a real answer i guess: What you describe is exactly the expected behaviour, according to the documentation of Geocoder: http://developer.android.com/reference/android/location/Geocoder.html#getFromLocation%28double,%20double,%20int%29
If you want to handle that case differently, can't you just catch that IO exception explicitly?
If you really want to get a valid address, you can only mirror the relevant lat, long in an offline DB - something i would not recommend.
Upvotes: 0