I am trying to get location of user by starting IntentService from MainActivity. Inside the service i try to reverse geocode the location inside a try block but when i catch the exception and print it says "Timed out waiting for server response" exception.But a few times I have got the location.so i think there is nothing wrong with my code.But it won't be useful if it throws exception 8 times out of 10.So can you suggest some thing to avoid this.
http://maps.googleapis.com/maps/api/geocode/json?latlng=lat,lng&sensor=true
Geocoder having the bug of time out waiting for server response .In alternate to that you can hit this request from your code to get the json response of location address of respective lat,lng.
I have suffered from, and got error due to unavailability of internet connection.
Please check have assign internet permission:
<uses-permission android:name="android.permission.INTERNET" />
And double check that your internet is ON or not.
And for the sake of getting response I write my code in AsyncTask like below:
class GeocodeAsyncTask extends AsyncTask<Void, Void, Address> {
String errorMessage = "";
@Override
protected void onPreExecute() {
progressBar.setVisibility(View.VISIBLE);
}
@Override
protected Address doInBackground(Void ... none) {
Geocoder geocoder = new Geocoder(DashboardActivity.this, Locale.getDefault());
List<Address> addresses = null;
double latitude = Double.parseDouble(strLatitude);
double longitude = Double.parseDouble(strLongitude);
try {
addresses = geocoder.getFromLocation(latitude, longitude, 1);
} catch (IOException ioException) {
errorMessage = "Service Not Available";
Log.e(TAG, errorMessage, ioException);
} catch (IllegalArgumentException illegalArgumentException) {
errorMessage = "Invalid Latitude or Longitude Used";
Log.e(TAG, errorMessage + ". " +
"Latitude = " + latitude + ", Longitude = " +
longitude, illegalArgumentException);
}
if(addresses != null && addresses.size() > 0)
return addresses.get(0);
return null;
}
protected void onPostExecute(Address address) {
if(address == null) {
progressBar.setVisibility(View.INVISIBLE);
tvcurrentLOc.setText(errorMessage);
}
else {
String addressName = "";
for(int i = 0; i < address.getMaxAddressLineIndex(); i++) {
addressName += "," + address.getAddressLine(i);
}
progressBar.setVisibility(View.INVISIBLE);
tvcurrentLOc.setText(addressName);
}
}
}
Hope it will help.
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