Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.81 KB | None | 0 0
  1. package android.javapapers.com.androidgeocodelocation;
  2.  
  3. import android.content.Context;
  4. import android.location.Address;
  5. import android.location.Geocoder;
  6. import android.os.Bundle;
  7. import android.os.Handler;
  8. import android.os.Message;
  9. import android.util.Log;
  10.  
  11. import java.io.IOException;
  12. import java.util.List;
  13. import java.util.Locale;
  14.  
  15. public class LocationAddress {
  16.     private static final String TAG = "LocationAddress";
  17.  
  18.     public static void getAddressFromLocation(final double latitude, final double longitude,
  19.                                               final Context context, final Handler handler) {
  20.         Thread thread = new Thread() {
  21.             @Override
  22.             public void run() {
  23.                 Geocoder geocoder = new Geocoder(context, Locale.getDefault());
  24.                 String result = null;
  25.                 try {
  26.                     List<Address> addressList = geocoder.getFromLocation(
  27.                             latitude, longitude, 1);
  28.                     if (addressList != null && addressList.size() > 0) {
  29.                         Address address = addressList.get(0);
  30.                         StringBuilder sb = new StringBuilder();
  31.                         for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
  32.                             sb.append(address.getAddressLine(i)).append("\n");
  33.                         }
  34.                         sb.append(address.getLocality()).append("\n");
  35.                         sb.append(address.getPostalCode()).append("\n");
  36.                         sb.append(address.getCountryName());
  37.                         result = sb.toString();
  38.                     }
  39.                 } catch (IOException e) {
  40.                     Log.e(TAG, "Unable connect to Geocoder", e);
  41.                 } finally {
  42.                     Message message = Message.obtain();
  43.                     message.setTarget(handler);
  44.                     if (result != null) {
  45.                         message.what = 1;
  46.                         Bundle bundle = new Bundle();
  47.                         result = "Latitude: " + latitude + " Longitude: " + longitude +
  48.                                 "\n\nAddress:\n" + result;
  49.                         bundle.putString("address", result);
  50.                         message.setData(bundle);
  51.                     } else {
  52.                         message.what = 1;
  53.                         Bundle bundle = new Bundle();
  54.                         result = "Latitude: " + latitude + " Longitude: " + longitude +
  55.                                 "\n Unable to get address for this lat-long.";
  56.                         bundle.putString("address", result);
  57.                         message.setData(bundle);
  58.                     }
  59.                     message.sendToTarget();
  60.                 }
  61.             }
  62.         };
  63.         thread.start();
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement