2013-04-20 71 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