Why is Android Geocoder throwing a “Service not Available” exception?

可紊 提交于 2019-11-26 13:21:56

I asked Google's Reto Meier to confirm my theory was correct and he said "Correct. The Geocoder is part of the Google API add-on that isn't part of the AOSP."

So any device that doesn't come with the Play Store, GMail apps etc… will also be missing the Geocoder back-end.

d60402

There seems to be another possible workaround for this problem, which is unfortunately marked as a duplicate question, and therefore might be missed. Essentially, a reboot of the device clears up the problem. Note I called it a "workaround" and not a "solution". :(

Megamind

For those who searching alternative, Hopefully, my answer in another post is useful. You can use Google Geocoding API when caught error in geocoding.

For more code => Get current location using json

Some devices do not have suport for Geocoder, so what you need to do is create your own geocoder.

Basicaly you need create a async task to request google for the address and treat the json response.

Using aquery, i do something like this:

public void asyncJson(String address){
        address = address.replace(" ", "+");

        String url = "http://maps.googleapis.com/maps/api/geocode/json?address="+ address +"&sensor=true";

        aq.ajax(url, JSONObject.class, new AjaxCallback<JSONObject>() {

                @Override
                public void callback(String url, JSONObject json, AjaxStatus status) {                        

                        if(json != null){

                                 //here you work with the response json
                                 JSONArray results = json.getJSONArray("results");                               
                                Toast.makeText(context, results.getJSONObject(1).getString("formatted_address"));

                        }else{                                
                                //ajax error, show error code
                                Toast.makeText(aq.getContext(), "Error:" + status.getCode(), Toast.LENGTH_LONG).show();
                        }
                }
        });        
}

After wasting several hours, I got a simplest solution.

I Just restarted my device, and it started working.

It seems, the problem is due to some OS level caching problem. Hope it will also work for your..

I had the same issue. I used the following function.

Note:

Use context of your Activity and don't use getApplicationContext() to the following function

public static Address getLocalityFrmGeoCoder(Context context, Location mLocation) {
        try {
            if(mLocation!=null){
            Geocoder gCoder = new Geocoder(context, Locale.getDefault());
            List<Address> address = gCoder.getFromLocation(mLocation.getLatitude(), mLocation.getLongitude(), 1);
            if (address.size() > 0) {
                return address.get(0);
            }
            }

        } catch (Exception e) {
            Log.d("GEOCODER", "GEOCODER EXCEPTION");
            e.printStackTrace();

        }
    return null;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!