SHARE
TWEET

Untitled

a guest Jul 22nd, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. public class FetchAddressTask extends AsyncTask<Location, Void, String>
  3. {
  4.  
  5.     private Context mContext;
  6.     private OnTaskCompleted mListener;
  7.  
  8.     interface OnTaskCompleted{
  9.         void onTaskCompleted(String result);
  10.     }
  11.    
  12.     FetchAddressTask(Context context, OnTaskCompleted listener){
  13.         mContext = context;
  14.         mListener = listener;
  15.     }
  16.    
  17.    
  18.     @Override
  19.     protected String doInBackground(Location... locations) {
  20.         Geocoder geocoder = new Geocoder(mContext, Locale.getDefault());
  21.         Location location = locations[0];
  22.         List<Address> addresses = null;
  23.         String resultMessage = "";
  24.         try {
  25.             addresses = geocoder.getFromLocation(
  26.                     location.getLatitude(),
  27.                     location.getLongitude(), 1); //1 banyaknya hasil
  28.            
  29.         } catch (IOException e) {
  30.             resultMessage = "Service not available";
  31.         }
  32.        
  33.         if(addresses == null || addresses.size() == 0){
  34.             resultMessage = "No Address Found";
  35.         }else{
  36.             Address addres = addresses.get(0);
  37.             ArrayList<String> addresPart = new ArrayList<>();
  38.             for (int i = 0; i < addres.getMaxAddressLineIndex(); i++) {
  39.                 addresPart.add(addres.getAddressLine(i));
  40.             }
  41.            
  42.             resultMessage = TextUtils.join("\n",addresPart);
  43.            
  44.         }
  45.         return  resultMessage;
  46.     }
  47.  
  48.     @Override
  49.     protected void onPostExecute(String s) {
  50.         mListener.onTaskCompleted(s);
  51.     }
  52. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top