2013-04-20 22 views
31

Beschreibung: Ich benutze Google maps API V2. Ich habe Android Reverse Geocoding an der berührten Stelle implementiert.android java.io.IOException: Service nicht verfügbar

Problem: Es wirft Ausnahme auf

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 latitude und longitude Werte korrekt empfing, aber ich kann nicht verstehen, warum es exception wirft, und ich habe auch Google-Suche durchgeführt, aber es kann nicht helfen .

Kann jemand bitte im Detail erklären ??

+0

Bitte aktualisieren Sie Ihre Frage mit kompletter Stapel. versuchen Sie diese http://stackoverflow.com/a/4762815/1329126 http://stackoverflow.com/questions/7109240/service-not-available-geocoder-android. –

+0

Laufen Sie im Emulator oder in einem echten Gerät? – Pratik

+0

@Sankar V - danke für die schnelle Antwort ... entsprechend der oben genannten Antwort angenommen - (jedes Gerät, das nicht mit dem Play Store, GMail-Apps usw. ... fehlt auch das Geocoder-Back-End.) Was es bedeutet?? – TheFlash

Antwort

16

Wenn Sie das Gerät verwenden, um einen Neustart löst nicht wollen, diese

 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)); 
+2

+ 1..Es hilft anderen Entwicklern. – TheFlash

+5

Das ist umgekehrt. Bei der ursprünglichen Frage ging es darum, die Adresse von LatLng zu erhalten. –

+0

Ist diese Google API eingeschränkt nutzbar? Wenn ja, wie viel Anfrage können wir einen Tag machen? –

8

I @Ani Lösung zu erhalten geändert Name der Stadt aus lat lange Parameter:

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; 
} 
+0

ML.log (apiRequest) ... was ist ML hier ?? – zyonneo

+0

Logger-Schnittstelle. Vergessen, es zu entfernen. #mybad –

+0

funktionierte nicht für mich – Pranita

Verwandte Themen