【问题标题】:Android Geocoder is not available?Android 地理编码器不可用?
【发布时间】:2013-12-05 07:22:09
【问题描述】:

我正在使用这种方法通过地址获取位置,并且我使用的是 android 4.2.2 设备。

addresses = g.getFromLocationName(addressString, 1);

当我第一次尝试获取地址位置时,它给了我实际结果。但几天后它说Geocoder 服务不可用。 我的问题是我使用了相同的设备,相同的 api 密钥并且它工作但现在它不工作。为什么? 如何解决这个问题。

【问题讨论】:

    标签: java android map


    【解决方案1】:

    这是 Google 本身的问题。

    重启设备即可解决问题。但这不是用户的最佳解决方案。

    你可以这样解决:

    public void getCityName(){ 
        if (running)
            return;
    
        new AsyncTask<Void, Void, String>(){
            String streetName = null;
            //String cityName = null;
    
            protected void onPreExecute(){
                running = true;
            };
    
            @Override
            protected String doInBackground(Void... params){
                if (Geocoder.isPresent()){ //if geocoder network is not crashed
                    try{
                        Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
                        List<Address> addresses = geocoder.getFromLocation(locationAll.getLatitude(), locationAll.getLongitude(), 1);
                        if (addresses.size() > 0){
                            cityName = addresses.get(0).getLocality();
                        }
                        for (Address address : addresses) {
                            System.out.println("my location .."+address.getAddressLine(0));
                            streetName = address.getAddressLine(0);
    
                        }
                    }
                    catch (Exception ignored){
                        // after a while, Geocoder start to throw "Service not available" exception. really weird since it was working before (same device, same Android version etc..
                    }
                }
    
                if (cityName != null){ // Geocoder succeed
    
                    return cityName;
                }
                else // Geocoder failed
                {
                    return fetchCityNameUsingGoogleMap();
                }
            }
    
            // Geocoder failed :-(
            // My B Plan : Google Map
            private String fetchCityNameUsingGoogleMap()
            {
                String googleMapUrl = "http://maps.googleapis.com/maps/api/geocode/json?latlng=" + locationAll.getLatitude() + ","
                        + locationAll.getLongitude() + "&sensor=false&language=fr";
    
    
                try
                {
                    JSONObject googleMapResponse = new JSONObject(ANDROID_HTTP_CLIENT.execute(new HttpGet(googleMapUrl),
                            new BasicResponseHandler()));
    
                    // many nested loops.. not great -> use expression instead
                    // loop among all results
                    JSONArray results = (JSONArray) googleMapResponse.get("results");
                    for (int i = 0; i < results.length(); i++)
                    {
                        // loop among all addresses within this result
                        JSONObject result = results.getJSONObject(i);
                        if (result.has("address_components"))
                        {
                            JSONArray addressComponents = result.getJSONArray("address_components");
                            // loop among all address component to find a 'locality' or 'sublocality'
                            for (int j = 0; j < addressComponents.length(); j++)
                            {
                                JSONObject addressComponent = addressComponents.getJSONObject(j);
                                if (result.has("types"))
                                {
                                    JSONArray types = addressComponent.getJSONArray("types");
    
                                    // search for locality and sublocality
                                    cityName = null;
    
                                    for (int k = 0; k < types.length(); k++)
                                    {
                                        if ("locality".equals(types.getString(k)) && cityName == null)
                                        {
                                            if (addressComponent.has("long_name"))
                                            {
                                                cityName = addressComponent.getString("long_name");
                                            }
                                            else if (addressComponent.has("short_name"))
                                            {
                                                cityName = addressComponent.getString("short_name");
                                            }
                                        }
                                        if ("sublocality".equals(types.getString(k)))
                                        {
                                            if (addressComponent.has("long_name"))
                                            {
                                                cityName = addressComponent.getString("long_name");
                                            }
                                            else if (addressComponent.has("short_name"))
                                            {
                                                cityName = addressComponent.getString("short_name");
                                            }
                                        }
                                    }
                                    if (cityName != null)
                                    {
    
                                        return cityName;
    
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception ignored)
                {
                    ignored.printStackTrace();
                }
                return null;
            }
    
            protected void onPostExecute(String cityName)
            {
                running = false;
                if (cityName != null)
                {
                    // Do something with cityName
                    Log.i("GeocoderHelper", cityName);
                }
            };     
    }.execute();
    
    }
    

    我只想要城市名称,您可以获取更多信息。

    如果Geocoder 不成功,它会通过HTTP 获取信息。

    【讨论】:

      【解决方案2】:

      你是否超过了最大值。 QUERY_LIMIT?当它停止工作时,您是否看到任何与此有关的日志?或者任何日志?

      【讨论】:

        【解决方案3】:

        请使用 api 级别和 google api 构建应用程序(例如 Google APIs (google Inc.) Api 级别 14)

        我在尝试调用 geoCoder.getFromLocationName(addressString, 1); 时遇到了同样的问题 要工作,我们需要重新启动设备。但这不是确切的解决方案。所以请在我的情况下尝试上述解决方案,它现在工作正常

        【讨论】:

          【解决方案4】:

          检查我的答案How to get the latitude and longitude from city name in android?

          我的建议是使用 Google GeoCoding REST API,而不是 Geocoder。

          因为我曾经在我的国家中国使用过,所以出现如下错误:

          "java.io.IOException: Service not Available"
          

          搜索后,好像是

          "The Geocoder class requires a backend service that is not included in the core android              framework. The Geocoder query methods will return an empty list if there no backend service in the platform. Use the isPresent() method to determine whether a Geocoder implementation    exists."
          

          所以我求助于 Google GeoCoding REST API,这很简单,只需像这样请求:

          http://maps.googleapis.com/maps/api/geocode/json?address=NewYork&sensor=false
          

          您将获得一个包含地理位置的 json。 您还可以通过它从 locaiton 获取地址:

          http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=false
          

          如果你会读中文,我有一个关于这个的博客,看看这个: http://wangchao.de/android%E5%88%A9%E7%94%A8google-geocoding-api%E6%9B%BF%E4%BB%A3geocoder-%E8%A7%A3%E5%86%B3%E5%9C%B0%E5%9D%80%E8%AF%AD%E8%A8%80%E9%97%AE%E9%A2%98

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-08-17
            • 1970-01-01
            • 2013-09-28
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-05-05
            • 1970-01-01
            相关资源
            最近更新 更多