As shown in previous lessons, location updates are stored in the form of latitude and longpolling coordinates. while this format is useful for calculating distance or displaying a pushpin on a map, the decimal numbers make no sense to most end users. if you need to display a location to user, it is much more preferable to display the address instead.
Perform Reverse Geocoding
Reverse-geocoding is the process of translating latitude longpolling coordinates to a human-readable address. the Geocoder API is available for this purpose. note that behind the scene, the API is dependent on a web service. if such service is unavailable on the device, the API will throw a "Service not Available exception" or return an empty list of addresses. A helper method called isPresent () was added in Android 2.3 (API level 9) to check for the existence of the service.
The following code snippet demonstrates the use of the Geocoder API to perform reverse-geocoding. since the getFromLocation () method is synchronous, you shocould not invoke it from the UI thread, hence an AsyncTask is used in the snippet.
Private final LocationListener listener = new LocationListener (){
Public void onLocationChanged (Location location ){
// Bypass reverse-geocoding if the Geocoder service is not available on
// Device. The isPresent () convenient method is only available on Gingerbread or abve.
If (Build. VERSION. SDK_INT> = Build. VERSION_CODES.GINGERBREAD & Geocoder. isPresent ()){
// Since the geocoding API is synchronous and may take a while. You don't want to lock
// Up the UI thread. Invoking reverse geocoding in an AsyncTask.
(New reversegeocodingtask(this)cmd.exe cute (new Location [] {location });
}
}
...
};
// AsyncTask encapsulating the reverse-geocoding API. Since the geocoder API is blocked,
// We do not want to invoke it from the UI thread.
Private class ReverseGeocodingTask extends AsyncTask <Location, Void, Void> {
Context mContext;
Public ReverseGeocodingTask (Context context ){
Super ();
MContext = context;
}
@ Override
Protected Void doInBackground (Location... params ){
Geocoder geocoder = new Geocoder (mContext, Locale. getDefault ());
Location loc = params [0];
List <Address> addresses = null;
Try {
// Call the synchronous getFromLocation () method by passing in the lat/long values.
Addresses = geocoder. getFromLocation (loc. getLatitude (), loc. getlongdistance (), 1 );
} Catch (IOException e ){
E. printStackTrace ();
// Update UI field with the exception.
Message. obtain (mHandler, UPDATE_ADDRESS, e. toString (). sendToTarget ();
}
If (addresses! = Null & s; addresses. size ()> 0 ){
Address address = addresses. get (0 );
// Format the first line of address (if available), city, and country name.
String addressText = String. format ("% s, % s, % s ",
Address. getMaxAddressLineIndex ()> 0? Address. getAddressLine (0 ):"",
Address. getLocality (),
Address. getCountryName ());
// Update the UI via a message handler.
Message. obtain (mHandler, UPDATE_ADDRESS, addressText). sendToTarget ();
}
Return null;
}
}