Search code examples
androidgeocodinggoogle-geocoderreverse-geocodingstreet-address

Geocoder blocks my android app


This is the code that I have:

private void getGeocoderForCheckout(int section) {
    Log.i("","current address getGeocoderForCheckout");
    setViewOver();
    loader.setVisibility(View.VISIBLE);
    sect = section;
    selectedSD = currentPlace;
    PSLocationCenter.getInstance().pref.setCheckoutType(context, "Current address");
    Geocoder geocoder = new Geocoder(context, Locale.getDefault());
    Log.i("","current address getGeocoderForCheckout");
    try {
        Log.i("","current address getGeocoderForCheckout1" + currentPlace);
        double lng = currentPlace.getLon();
        Log.i("","current address getGeocoderForCheckout1 lat lng: " + currentPlace.getLat() + "..." + lng);
        List<Address> address = geocoder.getFromLocation(currentPlace.getLat(), lng, 1);
        Log.i("","current address getGeocoderForCheckout1");
        if (address.size() > 0) {
            String title = address.get(0).getMaxAddressLineIndex() > 0 ? address.get(0).getAddressLine(0) : "";
            new PlacesAPI(context, currentAddressListener).execute(PlacesAPI.TYPE_AUTOCOMPLETE, "&input=" + URLEncoder.encode(title, "utf8"));
        }
    } catch (Exception e) {
        Log.i("","current address errorr: "+ e.getMessage());
        removeViewOver();
        Toast.makeText(context, context.getString(R.string.geocoder_error), Toast.LENGTH_SHORT).show();
        loader.setVisibility(View.GONE);
        Utils.appendLog("onmap long click errror : " + e.getMessage());
        e.printStackTrace();
    }
}

My issue is that it always blocks the app at this line:

List<Address> address = geocoder.getFromLocation(currentPlace.getLat(), lng, 1);

This is the log that I get back:

 09-11 12:22:59.725 25648-25648/nl.hgrams.passenger I/: current address has internet
 09-11 12:22:59.725 25648-25648/nl.hgrams.passenger I/: current address getGeocoderForCheckout
 09-11 12:22:59.729 25648-25648/nl.hgrams.passenger I/: current address getGeocoderForCheckout
 09-11 12:22:59.730 25648-25648/nl.hgrams.passenger I/: current address getGeocoderForCheckout1nl.hgrams.passenger.model.Destination@3c19358f
 09-11 12:22:59.731 25648-25648/nl.hgrams.passenger I/: current address getGeocoderForCheckout1 lat lng: 52.3724299...4.9088149

I tried to debug the app, but after entering the geocoder.getFromLocation line, it goest a bit, and stops, at the Looper. and from that moment, the app becomes unresponsive.

EDIT: If i press on the "getFromLocation" function to take me to it. It takes me into the Geocoder.java at this function:

 public List<Address> getFromLocation(double latitude, double longitude, int maxResults)
    throws IOException {
    if (latitude < -90.0 || latitude > 90.0) {
        throw new IllegalArgumentException("latitude == " + latitude);
    }
    if (longitude < -180.0 || longitude > 180.0) {
        throw new IllegalArgumentException("longitude == " + longitude);
    }
    try {
        List<Address> results = new ArrayList<Address>();
        String ex =  mService.getFromLocation(latitude, longitude, maxResults,
            mParams, results);
        if (ex != null) {
            throw new IOException(ex);
        } else {
            return results;
        }
    } catch (RemoteException e) {
        Log.e(TAG, "getFromLocation: got RemoteException", e);
        return null;
    }
}

WHERE:

 mService.getFromLocation(latitude, longitude, maxResults,
            mParams, results);

is in RED and says: "cannot resolve method"....

PS: I am using this function in other places too, and it works. So I have no ideea what might be wrong here.


Solution

  • Try to do the call of getFromLocation() in backgroud thread. From the documentation :

    It may be useful to call this method from a thread separate from your primary UI thread.