简体   繁体   English

Android Geocoder无法使用Android Nougat 7.0在Nexus 6P中运行

[英]Android Geocoder not working in Nexus 6P with Android Nougat 7.0

I can't get the reverse geocoding feature to work in Android 7.0. 我无法在Android 7.0中使用反向地理编码功能。 It was working fine in Marshmallow, and it's working correctly on another phone with kitkat. 它在Marshmallow工作正常,并且在另一部带有kitkat的手机上正常工作。

The error is a timeout in the request: 错误是请求中的超时:

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

The Geocoder isPresent method does return true. Geocoder isPresent方法确实返回true。

I solved this by making a request to the maps api 我通过向地图api发出请求来解决这个问题

And handled the json response like this: 并像这样处理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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM