Question

Can anyone tell me how reverse geocoder works. I have code which uses getFromlocation and getLastKnownLocation to find out the address. The thing is i don't know how to use the OnLocationChanged on it and how to disable the listener when i dont need it. I am utterly confused for the past 2 days. If any one can provide me some good resources with complete examples or link it would be great..

                Geocoder geocoder = new Geocoder(
                    TrackLogic.this.getApplicationContext(),
                    Locale.getDefault());
            Location locationGPS = locationManager
                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
            Location locationNetwork = locationManager
                    .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            Log.d(TAG, "geocoder works");
Was it helpful?

Solution

First you need to get a LocationManager instance and the name of the provider you want to use to do a location look-up. For example:

String provider = LocationManager.NETWORK_PROVIDER;
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

Next, you call requestLocationUpdates(...) like so:

locationManager.requestLocationUpdates(provider, 1000, 0, this);

This can be invoked on the main thread since (I think) the system will do the location lookup using the specified provider on a background thread. When a location is found, the Android system will invoke the onLocationChanged(...) callback, which you need to override. Since you're trying to do an address lookup, you would put in:

Geocoder geocoder = new Geocoder(this);
geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 10)

which should get you what you're looking for.

When you're done with using the Location services, you can call

locationManager.removeUpdates(this)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top