java - Android Geocoder getFromLocationName 因有效地址而失败

标签 java android google-geocoder

我正在尝试使用以下方法获取特定地址的纬度和经度 addressList = geoCoder.getFromLocationName(locationName, 1);

对于大多数地址,这工作得很好,但有一些有效地址,如“Lentoasemantie 1, Vantaa”,它返回空数组。奇怪的是,有效地址在 4 天前还可以使用,但现在大部分地址仍然可以使用。

所以,这看起来像是 Google 后端问题,我想知道我应该向 Google 报告这个问题(在何处/如何?)还是不再使用 Geocoder,因为它本质上是不可靠的?

最佳答案

Geocoder Android API 不如 Google Maps Geocoding API 高效,因此我建议您使用这个 API 而不是 Geocoder。 您可以在下面找到通过 Volley 队列从地址获取位置的函数(对于 Lentoasemantie 1,Vantaa,它有效):

public void getLocationFromAddress(String address) {
    String url = "https://maps.googleapis.com/maps/api/geocode/json?address="
            + Uri.encode(address) + "&sensor=true&key=API_KEY";
    RequestQueue queue = Volley.newRequestQueue(this);
    JsonObjectRequest stateReq = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            JSONObject location;
            try {
                // Get JSON Array called "results" and then get the 0th
                // complete object as JSON
                location = response.getJSONArray("results").getJSONObject(0).getJSONObject("geometry").getJSONObject("location");
                // Get the value of the attribute whose name is
                // "formatted_string"
                if (location.getDouble("lat") != 0 && location.getDouble("lng") != 0) {
                    LatLng latLng = new LatLng(location.getDouble("lat"), location.getDouble("lng"));

                    //Do what you want
                }
            } catch (JSONException e1) {
                e1.printStackTrace();

            }
        }

    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d("Error.Response", error.toString());
        }
    });
    // add it to the queue
    queue.add(stateReq);

}

关于java - Android Geocoder getFromLocationName 因有效地址而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39063590/

相关文章:

java - Java中哪种方法适合这种场景?

java - Google Maps Android API v2 ClassNotFound 运行时错误

Android - onStart 方法有什么问题?

r - 在 R 中进行地理编码时获取连接超时错误

python - TypeError : float() argument must be a string or a number, 不是 'method'

java - 使用 org.facebook4j 的 Facebook 身份验证错误

java - JUnitParamsRunner 与输入文件 - 字符串参数问题

android - 为 Android 生成 google-services.json

java - TTS : How to convert text into SSML?

android - Android Geocoder 是否仅适用于互联网连接?