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.
Asked
Active
Viewed 1.9k times
24
-
1There are loads of issues with the GeoCoder on Android, and the next one you're going to run into will be this https://code.google.com/p/android/issues/detail?id=38009 where the service just kills itself and the only solution is to restart the device, which is an entirely unacceptable solution... You're better off sending the request directly to google's geocoder website and parsing the data https://developers.google.com/maps/documentation/geocoding/ but, be warned, the data isn't always consistent and different countries define different attributes – Cruceo May 13 '14 at 17:58
-
Is it because there is some limits on how many requests we can send to server? – nayakasu May 13 '14 at 18:07
-
I'm sure it has some kind of limits, but I doubt that's your issue (unless you're spamming it, or something). But again, even if you didn't, you still going to undoubtedly run into the issue I mentioned above, so I would recommend implementing an alternative. – Cruceo May 13 '14 at 18:10
-
Here is an example of an alternative: http://stackoverflow.com/questions/19059894/google-geocoder-service-is-unavaliable-coordinates-to-address/19061688#19061688 – cYrixmorten May 13 '14 at 18:31
3 Answers
5
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.

Basheer Kohli
- 154
- 2
- 11
1
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.

KishuDroid
- 5,411
- 4
- 30
- 47
0
Here is a simple and working solution:
Create following two functions:
public static JSONObject getLocationInfo(String address) {
StringBuilder stringBuilder = new StringBuilder();
try {
address = address.replaceAll(" ","%20");
HttpPost httppost = new HttpPost("http://maps.google.com/maps/api/geocode/json?address=" + address + "&sensor=false");
HttpClient client = new DefaultHttpClient();
HttpResponse response;
stringBuilder = new StringBuilder();
response = client.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();
int b;
while ((b = stream.read()) != -1) {
stringBuilder.append((char) b);
}
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
JSONObject jsonObject = new JSONObject();
try {
jsonObject = new JSONObject(stringBuilder.toString());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jsonObject;
}
private static List<Address> getAddrByWeb(JSONObject jsonObject){
List<Address> res = new ArrayList<Address>();
try
{
JSONArray array = (JSONArray) jsonObject.get("results");
for (int i = 0; i < array.length(); i++)
{
Double lon = new Double(0);
Double lat = new Double(0);
String name = "";
try
{
lon = array.getJSONObject(i).getJSONObject("geometry").getJSONObject("location").getDouble("lng");
lat = array.getJSONObject(i).getJSONObject("geometry").getJSONObject("location").getDouble("lat");
name = array.getJSONObject(i).getString("formatted_address");
Address addr = new Address(Locale.getDefault());
addr.setLatitude(lat);
addr.setLongitude(lon);
addr.setAddressLine(0, name != null ? name : "");
res.add(addr);
}
catch (JSONException e)
{
e.printStackTrace();
}
}
}
catch (JSONException e)
{
e.printStackTrace();
}
return res;
}
Now Simply replace
geocoder.getFromLocation(locationAddress, 1);
with
getAddrByWeb(getLocationInfo(locationAddress));

user3242316
- 33
- 1
-
2Calling the API directly via HTTP instead of the SDK may result in significant increase of the Google API charges. Unless of course the API is free for your puprose, but often it is not. But the SDK is free. – Hermann Klecker May 21 '15 at 12:27
-
so using Geocoder android api is better or not than calling the http api? – Daniel Gomez Rico May 22 '15 at 21:47
-
This answer gets a location from an address, not a lat/long, which is what getFromLocation does. – Aphex Jan 15 '16 at 09:18
-
that answer not return getLocality(). but any how return getAddressLine(0) – Munir Feb 28 '17 at 12:45