0

I want to return a string array from Async class back to the activity that is calling this asynchronous class that is job is to do the reverse geocoding.


So, from my activity I call the constructor of the class like this:

Double[] lat_long = new Double[] { Double.parseDouble(map_lat), Double.parseDouble(map_long) };

            ReverseGeocodingTask reverseGeocoding = new ReverseGeocodingTask(getActivity().getApplicationContext());
            reverseGeocoding.execute(lat_long);

And this is the code of the class:

class ReverseGeocodingTask extends AsyncTask<Double, Void, List<String>> {

    public static List<String> LIST = new ArrayList<String>();

    Context mContext;

    public ReverseGeocodingTask(Context context) {
        super();
        mContext = context;
    }
    @Override
    protected List<String> doInBackground(Double... params) {
        Geocoder gc= new Geocoder(mContext, Locale.getDefault());

        List<Address> addrList = null;

        double latitude = params[0].doubleValue();
        double longitude = params[1].doubleValue();

        Log.d("LATLONG", latitude + ":" + longitude);

        try {
            addrList = gc.getFromLocation(latitude, longitude, 1);

            if (addrList.size() > 0) {
                //format location info
                Address address = addrList.get(0);

                LIST.add(address.getLocality());
                LIST.add(address.getSubAdminArea());
                LIST.add(address.getCountryName());

                Log.d("LIST", LIST.get(0));
            }
            else{
                Log.d("addrList SIZE", "=0");
                return null;
            }
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

        return LIST;
    }

    @Override
    protected void onPostExecute(List<String> result) {
        if (result != null) {
            Log.d("ON POST", result.get(0));
        }
    }
}

This is the logcat:

02-28 19:20:04.323  12275-14109/guide_me_for_all.guide_me_for_all D/LATLONG﹕ 34.681377999999995:33.039339
02-28 19:20:05.434  12275-14109/guide_me_for_all.guide_me_for_all D/addrList SIZE﹕ =0

I get correctly the latitude and longitude point as you can see from the Log.d(), BUT getFromLocation.size() is always 0.

Marialena
  • 817
  • 8
  • 31

1 Answers1

0

This may be a problem with your GeoCoder service. If you're backend service for the device is not present or has other problems, you will get this response.

use isPresent to check if an implementation is present.

Also, see this post here:

Geocoder.getFromLocation throws IOException on Android emulator

And the docs mention that you need a backend service:

http://developer.android.com/reference/android/location/Geocoder.html

Community
  • 1
  • 1
Jim
  • 10,172
  • 1
  • 27
  • 36
  • I added this check: `if (gc.isPresent()) {` and the geocoder is present indeed. Also, I am using geocoder and `GetFromlocationName()` instead of `GetFromLocation()` in another activity and another AsyncTask and it's working fine. But that whole solution about `GetFomLocationName()` was using also `HttpPost` etc. I just didn't know how to change that class to make it work for `GetFromLocation()` too. – Marialena Feb 28 '15 at 18:25