-1

I'm trying to fetch the address of the user selected location from the Google maps. For fetching the address I'm using GeoCoder.

        geoCoder.getFromLocation(location)

Always returns my current location address, even when the location changes.

Note: I'm calling this method onLocationChange()

Updated:

But for the same location, when I send location from GoogleMaps.OnLongClickListener it works fine. It doesn't work only on LocationListener.onLocationChanged.

Kamalakannan J
  • 2,818
  • 3
  • 23
  • 51

2 Answers2

0

use below method to get address of a geo location -

public static String reverseGeoCoder(Context mContext,double lat,double longi){
    try{
        Geocoder geoCoder = new Geocoder(mContext);
        List<Address> matches = geoCoder.getFromLocation(lat, longi, 1);
        Address bestMatch = (matches.isEmpty() ? null : matches.get(0));
        String address = bestMatch.getAddressLine(0)+" "+bestMatch.getAddressLine(1);
        return address;
    }
    catch(Exception e){
        e.printStackTrace();
    }
    return "";
}
Ravi Bhandari
  • 4,682
  • 8
  • 40
  • 68
0

Try the below code. The problem could be with how you are using your LocationListener.

LocationManger lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 100, new LocationListener() {
    @Override
        public void onLocationChanged(Location location) {
            StringBuilder result = new StringBuilder();
    try {
        Geocoder geocoder = new Geocoder(this, Locale.getDefault());
        List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
        if (addresses.size() > 0) {
            Address address = addresses.get(0);
            result.append(address.getLocality()).append("\n");
            result.append(address.getCountryName());
        }
     }catch (IOException e) {
        Log.e("tag", e.getMessage());
    }

    }
    }
Exception
  • 2,273
  • 1
  • 24
  • 42