本文介绍了Android Geocoder getFromLocationName失败并显示有效地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to get latitude and longitude of specific addresses using addressList = geoCoder.getFromLocationName(locationName, 1);

对于大多数地址而言工作得很好,但有一些有效的地址,如Lentoasemantie 1,Vantaa,它返回空数组。奇怪的是,有效地址过去常常在4天前工作,但现在大部分地址继续有效。

For most addresses this works just fine, but there are some valid addresses, like "Lentoasemantie 1, Vantaa", which returns empty array. The strange thing is that the valid addresses used to work 4 days ago, but not anymore while most of the addresses continue to work.

所以,这看起来像Google后端问题而且我想知道我应该向谷歌报告(在哪里/如何?)或者不再使用Geocoder,因为它本身就不可靠?

So, this looks like Google backend problem and I'm wondering should I report this to Google (where / how?) or switch away from using Geocoder, because it's inherently unreliable?

推荐答案

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

Geocoder Android API is not as efficient as the Google Maps Geocoding API so I suggest you to use this one instead of Geocoder.You can find below the function to get location from address through Volley queue (For Lentoasemantie 1, Vantaa, it works) :

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);

}

这篇关于Android Geocoder getFromLocationName失败并显示有效地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-04 08:01