Home | History | Annotate | Download | only in location
      1 page.title=Displaying the Location Address
      2 parent.title=Making Your App Location Aware
      3 parent.link=index.html
      4 
      5 trainingnavtop=true
      6 previous.title=Obtaining the Current Location
      7 previous.link=currentlocation.html
      8 
      9 @jd:body
     10 
     11 
     12 <!-- This is the training bar -->
     13 <div id="tb-wrapper">
     14 <div id="tb">
     15 
     16 <h2>This lesson teaches you to</h2>
     17 <ol>
     18   <li><a href="geocoding.html#TaskReverseGeocoding">Perform Reverse Geocoding</a></li>
     19 </ol>
     20 
     21 <h2>You should also read</h2>
     22 
     23 <ul>
     24   <li><a href="{@docRoot}guide/topics/location/index.html">Location and Maps</a></li>
     25 </ul>
     26 
     27 <h2>Try it out</h2>
     28 
     29 <div class="download-box">
     30 <a href="http://developer.android.com/shareables/training/LocationAware.zip" class="button">Download
     31   the sample app</a>
     32 <p class="filename">LocationAware.zip</p>
     33 </div>
     34 
     35 </div>
     36 </div>
     37 
     38 <p>As shown in previous lessons, location updates are received in the form of latitude and longitude 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.</p>
     39 
     40 <h2 id="TaskReverseGeocoding">Perform Reverse Geocoding</h2>
     41 
     42 <p>Reverse-geocoding is the process of translating latitude longitude coordinates to a human-readable address.  The {@link android.location.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 {@link android.location.Geocoder#isPresent()} was added in Android 2.3 (API level 9) to check for the existence of the service.</p>
     43 
     44 <p>The following code snippet demonstrates the use of the {@link android.location.Geocoder} API to perform reverse-geocoding.  Since the {@link android.location.Geocoder#getFromLocation(double, double, int) getFromLocation()} method is synchronous, you should not invoke it from the UI thread, hence an {@link android.os.AsyncTask} is used in the snippet.</p>
     45 
     46 <pre>
     47 private final LocationListener listener = new LocationListener() {
     48 
     49     public void onLocationChanged(Location location) {
     50         // Bypass reverse-geocoding if the Geocoder service is not available on the
     51         // device. The isPresent() convenient method is only available on Gingerbread or above.
     52         if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.GINGERBREAD &amp;&amp; Geocoder.isPresent()) {
     53             // Since the geocoding API is synchronous and may take a while.  You don't want to lock
     54             // up the UI thread.  Invoking reverse geocoding in an AsyncTask.
     55             (new ReverseGeocodingTask(this)).execute(new Location[] {location});
     56         }
     57     }
     58     ...
     59 };
     60 
     61 // AsyncTask encapsulating the reverse-geocoding API.  Since the geocoder API is blocked,
     62 // we do not want to invoke it from the UI thread.
     63 private class ReverseGeocodingTask extends AsyncTask&lt;Location, Void, Void&gt; {
     64     Context mContext;
     65 
     66     public ReverseGeocodingTask(Context context) {
     67         super();
     68         mContext = context;
     69     }
     70 
     71     &#064;Override
     72     protected Void doInBackground(Location... params) {
     73         Geocoder geocoder = new Geocoder(mContext, Locale.getDefault());
     74 
     75         Location loc = params[0];
     76         List&lt;Address&gt; addresses = null;
     77         try {
     78             // Call the synchronous getFromLocation() method by passing in the lat/long values.
     79             addresses = geocoder.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1);
     80         } catch (IOException e) {
     81             e.printStackTrace();
     82             // Update UI field with the exception.
     83             Message.obtain(mHandler, UPDATE_ADDRESS, e.toString()).sendToTarget();
     84         }
     85         if (addresses != null &amps;&amps; addresses.size() &gt; 0) {
     86             Address address = addresses.get(0);
     87             // Format the first line of address (if available), city, and country name.
     88             String addressText = String.format("&#037;s, &#037;s, &#037;s",
     89                     address.getMaxAddressLineIndex() &gt; 0 ? address.getAddressLine(0) : "",
     90                     address.getLocality(),
     91                     address.getCountryName());
     92             // Update the UI via a message handler.
     93             Message.obtain(mHandler, UPDATE_ADDRESS, addressText).sendToTarget();
     94         }
     95         return null;
     96     }
     97 }
     98 </pre>