3

我無法使用反向地理編碼功能在Android 7.0中工作。它在棉花糖中工作正常,並且在另一部帶有kitkat的手機上正常工作。Android地理編碼器不支持Nexus 6P與Android Nougat 7.0

的錯誤是在請求超時:

java.io.IOException: Timed out waiting for response from server 
    at android.location.Geocoder.getFromLocation(Geocoder.java:136) 

地址解析器isPresent方法並返回true。

回答

0

我解決了這個通過向maps api

的請求,並處理這樣的JSON響應:

public void onResponse(JSONObject response) { 
        JsonArray results; 
        try { 
         results = ((JsonArray)new JsonParser().parse(response.get("results").toString())); 
        } catch (JSONException e) { 
         e.printStackTrace(); 
         return; 
        } 
        String country = ""; 
        String stateProvince = ""; 
        String locality = ""; 
        String hood = ""; 
        if (results.size() > 0) { 
         JsonArray address = results.get(0).getAsJsonObject().get("address_components").getAsJsonArray(); 
         for (JsonElement component : address) { 
          JsonObject data = component.getAsJsonObject(); 
          for (JsonElement type : data.get("types").getAsJsonArray()) { 
           if (type.getAsString().equals("country")) { 
            country = data.get("short_name").getAsString(); 
           } else if (type.getAsString().equals("administrative_area_level_1")) { 
            stateProvince = data.get("short_name").getAsString(); 
           } else if (type.getAsString().equals("locality")) { 
            locality = data.get("long_name").getAsString(); 
           } else if (type.getAsString().equals("sublocality")) { 
            hood = data.get("long_name").getAsString(); 
           } 
          } 
         } 
        } 
        final String address = getFormattedAddress(country, stateProvince, locality, hood); 
} 

public String getFormattedAddress(String country, String state, String locality, String hood) { 
    String address = ""; 
    if(hood.isEmpty()){ 
     if(locality.isEmpty()){ 
      if(!state.isEmpty()){ 
       address += state; 
      } 
     }else{ 
      address += locality; 
      if(!state.isEmpty()){ 
       address += ", " + state; 
      } 
     } 
    }else{ 
     address = hood; 
     if(locality.isEmpty()){ 
      if(!state.isEmpty()){ 
       address += ", " + state; 
      } 
     }else{ 
      address += ", " + locality; 
     } 
    } 
    if(!country.isEmpty()){ 
     if(!address.isEmpty()){ 
      address += ", " + country; 
     }else{ 
      address += country; 
     } 
    } 
    return address; 
}