0

I get the latitude and longtitude but I can't do the reverse geocoding, I have seen so many tutorials and they're working right but mine is not!

Here's my code:

    if (mLastLocation != null) {
        double latitude = mLastLocation.getLatitude();
        double longitude = mLastLocation.getLongitude();
        txtCoordinates.setText(latitude + " / " + longitude);


        Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
        try {
            List<Address> addresses = geoCoder.getFromLocation(latitude, longitude, 1);

            String strCompleteAddress= "";
            if (addresses.size() > 0)
            {
                for (int i=0; i<addresses.get(0).getMaxAddressLineIndex();i++)
                    strCompleteAddress+= addresses.get(0).getAddressLine(i) + "\n";
            }
            Log.i("MyLocTAG => ", strCompleteAddress);
            Toast.makeText(getBaseContext(), strCompleteAddress, Toast.LENGTH_LONG).show();
        }
        catch (IOException e) {
            Log.i("MyLocTAG => ", "this is the exception part");
            e.printStackTrace();
        }

This is my IOException!

W/System.err: java.io.IOException: Timed out waiting for response from server
W/System.err:     at android.location.Geocoder.getFromLocation(Geocoder.java:136)
W/System.err:     at com.example.beultimate.location.MainActivity.displayLocation(MainActivity.java:136)
W/System.err:     at com.example.beultimate.location.MainActivity.onLocationChanged(MainActivity.java:227)
W/System.err:     at com.google.android.gms.internal.zzart$zzb$1.zza(Unknown Source:2)
W/System.err:     at com.google.android.gms.internal.zzart$zzb$1.zzs(Unknown Source:2)
W/System.err:     at com.google.android.gms.internal.zzaaz.zzb(Unknown Source:8)
W/System.err:     at com.google.android.gms.internal.zzaaz$zza.handleMessage(Unknown Source:14)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:106)
W/System.err:     at android.os.Looper.loop(Looper.java:164)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6494)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
I/Choreographer: Skipped 300 frames!  The application may be doing too much work on its main thread.

I also have another version of this code wherein I am getting a nullpointerexception, because the list is empty!

Rino
  • 1,215
  • 1
  • 16
  • 28
Erika
  • 148
  • 3
  • 14
  • 1
    Possible duplicate of [Android Geocoder behaves different on some devices](https://stackoverflow.com/questions/47070264/android-geocoder-behaves-different-on-some-devices) – ישו אוהב אותך Feb 26 '18 at 12:42

2 Answers2

2

Sometime Geocoder class cannot return values, to overcome this problem, use,

        //Get address from Google api
        //Log.e("Else","else");
        try {
            JSONObject jsonObj = getJSONfromURL("http://maps.googleapis.com/maps/api/geocode/json?latlng=" + latitude + ","
                    + longitude + "&sensor=true");// to getting address form latitude and longitude
            String Status = jsonObj.getString("status");
            if (Status.equalsIgnoreCase("OK")) {
                JSONArray Results = jsonObj.getJSONArray("results");
                JSONObject location = Results.getJSONObject(0);
                finalAddress = location.getString("formatted_address");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

This URL returns JSON result.

RoHiT
  • 372
  • 3
  • 12
1

Try something like this:

public String getAddress(double lat, double lng) {
    Geocoder geocoder = new Geocoder(this, Locale.getDefault());
    try {
        List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
        Address obj = addresses.get(0);
        return obj.getSubLocality();
    } catch (Exception e) {
        e.printStackTrace();
        Toast.makeText(this, "Could not get address, please try again later", Toast.LENGTH_SHORT).show();
        return null;
    }
}

Hope it helps!

amit srivastava
  • 743
  • 6
  • 25