5

I have to get an address by coordinates, but it doesnt work and outputs "Couldnt get address":

public String GetAddress(String lat, String lon)
        {
            Geocoder geocoder = new Geocoder(this, Locale.ENGLISH);
            String ret = "";
            try {
                List<Address> addresses = geocoder.getFromLocation(Double.parseDouble(lat), Double.parseDouble(lon), 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");
                    }
                    ret = strReturnedAddress.toString();
                    ret=ret.substring(0, ret.length() - 1);
                }
                else{
                    ret = "Unknown Address";
                }
            }
            catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                ret = "Couldn't get Address";
            }
            return ret;
        }

LogCat:

09-27 23:47:49.837: W/System.err(1862): java.io.IOException: Service not Available
09-27 23:47:49.847: W/System.err(1862):     at android.location.Geocoder.getFromLocation(Geocoder.java:136)
09-27 23:47:49.847: W/System.err(1862):     at com.example.free.Add.GetAddress(Add.java:634)
09-27 23:47:49.857: W/System.err(1862):     at com.example.free.Add$LoadAddress.doInBackground(Add.java:608)
09-27 23:47:49.857: W/System.err(1862):     at com.example.free.Add$LoadAddress.doInBackground(Add.java:1)
09-27 23:47:49.857: W/System.err(1862):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
09-27 23:47:49.857: W/System.err(1862):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-27 23:47:49.857: W/System.err(1862):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-27 23:47:49.857: W/System.err(1862):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
09-27 23:47:49.857: W/System.err(1862):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
09-27 23:47:49.857: W/System.err(1862):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
09-27 23:47:49.857: W/System.err(1862):     at java.lang.Thread.run(Thread.java:856)
mn6vdv23g
  • 734
  • 2
  • 10
  • 33
  • http://stackoverflow.com/questions/7109240/service-not-available-geocoder-android – nhgrif Sep 27 '13 at 20:57
  • excuse me. its a wierd problem.. – mn6vdv23g Sep 27 '13 at 21:18
  • Did you follow to [this](http://stackoverflow.com/questions/4761052/why-is-android-geocoder-throwing-a-service-not-available-exception) link? – nhgrif Sep 27 '13 at 21:42
  • I think the bigger question is ... WHY has this suddenly stopped working? I had this working fine 2 weeks ago and it is only during regression testing that it was raised. How can an entire service just stop working. There must be apps in the public domain that are now fundamentally broken? If this is a matter of subsription level or allowed usage being exceeded then that detail should be returned from the service. – ComeIn Jun 08 '16 at 01:12

1 Answers1

14

The common answer to this problem is that you have to restart your device.

Surely you cannot tell your users to restart device in order for your app to function, so my solution was to use a HTTP fallback, here is the AsyncTask I use in my code.

You will have to modify it for your situation, as I lookup address from position and not the other way around.

    private class GetAddressPositionTask extends
        AsyncTask<String, Integer, LatLng> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected LatLng doInBackground(String... plookupString) {

        String lookupString = plookupString[0];
        final String lookupStringUriencoded = Uri.encode(lookupString);
        LatLng position = null;

        // best effort zoom
        try {
            if (geocoder != null) {
                List<Address> addresses = geocoder.getFromLocationName(
                        lookupString, 1);
                if (addresses != null && !addresses.isEmpty()) {
                    Address first_address = addresses.get(0);
                    position = new LatLng(first_address.getLatitude(),
                            first_address.getLongitude());
                }
            } else {
                Log.e(TAG, "geocoder was null, is the module loaded? "
                        + isLoaded);
            }

        } catch (IOException e) {
            Log.e(TAG, "geocoder failed, moving on to HTTP");
        }
        // try HTTP lookup to the maps API
        if (position == null) {
            HttpGet httpGet = new HttpGet(
                    "http://maps.google.com/maps/api/geocode/json?address="
                            + lookupStringUriencoded + "&sensor=true");
            HttpClient client = new DefaultHttpClient();
            HttpResponse response;
            StringBuilder stringBuilder = new StringBuilder();

            try {
                response = client.execute(httpGet);
                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 {
                // Log.d("MAPSAPI", stringBuilder.toString());

                jsonObject = new JSONObject(stringBuilder.toString());
                if (jsonObject.getString("status").equals("OK")) {
                    jsonObject = jsonObject.getJSONArray("results")
                            .getJSONObject(0);
                    jsonObject = jsonObject.getJSONObject("geometry");
                    jsonObject = jsonObject.getJSONObject("location");
                    String lat = jsonObject.getString("lat");
                    String lng = jsonObject.getString("lng");

                    // Log.d("MAPSAPI", "latlng " + lat + ", "
                    // + lng);

                    position = new LatLng(Double.valueOf(lat),
                            Double.valueOf(lng));
                }

            } catch (JSONException e) {
                Log.e(TAG, e.getMessage(), e);
            }

        }
        return position;
    }

    @Override
    protected void onPostExecute(LatLng result) {
        Log.i("GEOCODE", result.toString());
        super.onPostExecute(result);
    }

};
cYrixmorten
  • 7,110
  • 3
  • 25
  • 33
  • 1
    It is just the search string which uri encoded (whitespaces replaced with %20 etc). I use it in the HTTP fallback for the query parameter. – cYrixmorten Feb 27 '14 at 12:32
  • Probably not worth attempting the API call first, given it is known to be flakey. Just use HTTP. You don;t want the user hanging around for a connection timeout before your app makes the http call. – ComeIn Jun 07 '16 at 15:30
  • @ComeIn A valid point, however the failure is rather instant, so does not impose any delay. In case you are working on geocoding at the moment, I have participated in this project: https://github.com/mcharmas/Android-ReactiveLocation where there recently have been implemented a HTTP fallback as well. – cYrixmorten Jun 08 '16 at 07:37