2013-04-20 36 views
31

描述:我正在使用Google maps API V2。我在觸摸位置實現了Android Reverse Geocodingandroid java.io.IOException:服務不可用

問題:它輕視

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

我收到latitudelongitude值正確的異常,但我不明白爲什麼它會拋出exception,我也做了谷歌搜索,但它不能幫助。

任何人都可以請詳細解釋??

+0

請更新您的問題與完整的堆棧跟蹤。試試這個http://stackoverflow.com/a/4762815/1329126 http://stackoverflow.com/questions/7109240/service-not-available-geocoder-android。 – 2013-04-20 10:18:52

+0

您是在模擬器還是在真實設備中運行? – Pratik 2013-04-20 10:20:57

+0

@Sankar V - 感謝您的快速回復......根據上述鏈接中提到的接受的答案 - (任何未隨Play商店,GMail應用程序等提供的設備......也將缺少Geocoder後端。)這是什麼意思?? – TheFlash 2013-04-20 10:22:58

回答

16

如果你不想重啓設備使用這種

 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 ..它會幫助其他開發人員。 – TheFlash 2014-02-27 08:44:42

+5

這是另一回合。原來的問題是關於從LatLng獲取地址。 – 2014-07-16 09:48:35

+0

這是谷歌api使用有限嗎?如果是的話,我們可以做多少天的要求? – 2017-12-13 17:36:32

8

我修改@Ani解決方案,使城市名稱從lat長參數:

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)...什麼是ML? – zyonneo 2015-03-25 05:08:44

+0

記錄器接口。忘了刪除它。 #mybad – 2015-03-30 07:44:55

+0

對我無效 – Pranita 2017-11-23 10:33:34