31
votes

The app uses the Geocoder object. It works fine on my stock Froyo Nexus One. But then I run the exact same app on a different device (an Advent Vega 10" tablet also running Froyo) and I get this exception: Service not Available. The method I'm using is getFromLocationName(), I'm building against the Android 1.6 Google API.

I'm aware of an issue where this exception is thrown on the emulator, but I suspect this is different. Why would it be thrown on one device running Froyo but not another?

The app is a location app, and as the tablet has no GPS or mobile network, in a scenario where the Wi-Fi connection doesn't provide a location, the user must manually specify it, so not being able to use the Geocoder object is bad news.

I could add a way for the user to select the location on a map, but it's not ideal. Possibly I could use the Google Maps API directly, but I'm keen to understand the nature of the issue first as would be nice to have an easier solution.

Hopefully in a future release Android will include an OS-level "default location" for non-Geocoder devices, so location-aware apps work out of the box on devices like Google TV.

6
I see in the docs it says "The Geocoder class requires a backend service that is not included in the core android framework" but why would a device vendor not include it? A mistake? - Ollie C
Is the Geocoder back-end considered part of the approved-by-Google Android distribution? The tablet is not Google-certified so no Google Android apps (GMail, Market etc) on it by default. I suspect this is why. Looks like people on these types of devices may have to pick location off a map, or I could maybe use that Maps API directly. Seems weird to exclude it, would love to know why it's not there. - Ollie C

6 Answers

27
votes

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.

13
votes

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". :(

2
votes

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

1
votes

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();
                        }
                }
        });        
}
1
votes

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..

0
votes

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