38

Description: I am using Google maps API V2.I have implemented Android Reverse Geocoding at touched location.

Problem: It throws exception on

 try {
     addresses = geocoder.getFromLocation(latitude, longitude,1);}
 catch (IOException e)
     {
     e.printStackTrace();
     if(AppConstants.DEBUG)Log.v(AppConstants.DEBUG_TAG," e.printStackTrace(): "+e.getMessage());
     }

I am receiving latitude and longitude values correct, but i can't understand why it throws exception and i have also done Google search but it couldn't help.

Can anybody please explain in details??

Vikalp Patel
  • 10,669
  • 6
  • 61
  • 96
TheFlash
  • 5,997
  • 4
  • 41
  • 46
  • Please update your question with complete stacktrace. try this http://stackoverflow.com/a/4762815/1329126 http://stackoverflow.com/questions/7109240/service-not-available-geocoder-android. – Sankar V Apr 20 '13 at 10:18
  • Are you running in emulator or in real device? – Pratik Apr 20 '13 at 10:20
  • @Sankar V - thanks for quick reply... according to accepted answer mentioned in above link -- (any device that doesn't come with the Play Store, GMail apps etc… will also be missing the Geocoder back-end.)What it means?? – TheFlash Apr 20 '13 at 10:22
  • @ Pratik -no i m using device. (any device that doesn't come with the Play Store, GMail apps etc… will also be missing the Geocoder back-end.)can you explain me What it means?? – TheFlash Apr 20 '13 at 10:23
  • Its clear in that, if there is no Google apps like PlayStore, GMail in device while purchasing, the devices lacks Geocoder too – Sankar V Apr 20 '13 at 10:29
  • Sankar V- i m using Samsung Galaxy Note 800...! – TheFlash Apr 20 '13 at 10:31
  • @patelpratik we really need your logcat stacktrace to identify the exact problem. Before that try rebooting your device and try – Sankar V Apr 20 '13 at 10:35
  • @Sankar V-There is nothing special in stacktrace that's why i have post the 1 line that cause exception..i will reboot my device then i will tell u if it works..!thanks again. – TheFlash Apr 20 '13 at 10:42
  • @Sankar V-Thanx a lot Rebooting worked.post your answer so that i can accept it..! – TheFlash Apr 20 '13 at 11:24
  • Facing same issue. – IntelliJ Amiya Aug 03 '18 at 09:24

3 Answers3

86

As stated in this Android issue 38009 - Geocoder throwing exception: IOException: Service not Available Rebooting the Device will solve the problem

Sankar V
  • 4,794
  • 3
  • 38
  • 56
  • 2
    but after some time it will again show error. Is there any good solution for that? – AmmY Mar 27 '14 at 11:15
  • Restarted device solved the issue but what is the best way to avoid it? – Shailendra Madda Apr 05 '18 at 09:58
  • @ShylendraMadda Nothing. The issue is still open in bug tracker - https://issuetracker.google.com/issues/36956130. May be you can go with the answer below - https://stackoverflow.com/a/22061612/1329126 – Sankar V Apr 12 '18 at 18:22
16

If you don't want to reboot the device use this

     public  JSONObject getLocationFormGoogle(String placesName) {

    HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?address=" +placesName+"&ka&sensor=false");
    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 {
        jsonObject = new JSONObject(stringBuilder.toString());
    } catch (JSONException e) {

        e.printStackTrace();
    }

    return jsonObject;
}

public  LatLng getLatLng(JSONObject jsonObject) {

    Double lon = new Double(0);
    Double lat = new Double(0);

    try {

        lon = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
            .getJSONObject("geometry").getJSONObject("location")
            .getDouble("lng");

        lat = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
            .getJSONObject("geometry").getJSONObject("location")
            .getDouble("lat");

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return new LatLng(lat,lon);

}



LatLng Source =getLatLng(getLocationFormGoogle(placesName));
Sankar V
  • 4,794
  • 3
  • 38
  • 56
Anil M H
  • 3,332
  • 5
  • 34
  • 51
10

I modified @Ani solution to get city name from lat long parameters:

public static String getLocationCityName( double lat, double lon ){
    JSONObject result = getLocationFormGoogle(lat + "," + lon );
    return getCityAddress(result);
}

protected static JSONObject getLocationFormGoogle(String placesName) {

    String apiRequest = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + placesName; //+ "&ka&sensor=false"
    HttpGet httpGet = new HttpGet(apiRequest);
    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 {
        jsonObject = new JSONObject(stringBuilder.toString());
    } catch (JSONException e) {

        e.printStackTrace();
    }

    return jsonObject;
}

protected static String getCityAddress( JSONObject result ){
    if( result.has("results") ){
        try {
            JSONArray array = result.getJSONArray("results");
            if( array.length() > 0 ){
                JSONObject place = array.getJSONObject(0);
                JSONArray components = place.getJSONArray("address_components");
                for( int i = 0 ; i < components.length() ; i++ ){
                    JSONObject component = components.getJSONObject(i);
                    JSONArray types = component.getJSONArray("types");
                    for( int j = 0 ; j < types.length() ; j ++ ){
                        if( types.getString(j).equals("locality") ){
                            return component.getString("long_name");
                        }
                    }
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    return null;
}
Sankar V
  • 4,794
  • 3
  • 38
  • 56
Pawel Cala
  • 685
  • 9
  • 14