Advertisement
GeorgePashev_88

Untitled

May 15th, 2023
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.87 KB | None | 0 0
  1. package com.example.kontakti;
  2.  
  3.  
  4.  
  5.         import android.content.Context;
  6.         import android.location.Address;
  7.         import android.location.Geocoder;
  8.         import android.os.Bundle;
  9.         import android.os.Handler;
  10.         import android.os.Message;
  11.         import android.util.Log;
  12.  
  13.         import java.io.IOException;
  14.         import java.util.List;
  15.         import java.util.Locale;
  16.  
  17. public class GeoCodingLocation {
  18.  
  19.     private static final String TAG = "GeocodingLocation";
  20.  
  21.     public interface LatLngGet{
  22.         void getLatLng(double lat, double lon);
  23.         void ErrorMessage(String error);
  24.     }
  25.  
  26.     public static void getAddressFromLocation(final String locationAddress,
  27.                                               final Context context, final LatLngGet callback) {
  28.         Thread thread = new Thread() {
  29.             @Override
  30.             public void run() {
  31.                 Geocoder geocoder = new Geocoder(context, Locale.getDefault());
  32.  
  33.                 try {
  34.                     List<Address>
  35.                             addressList = geocoder.getFromLocationName(locationAddress, 1);
  36.                     if (addressList != null && addressList.size() > 0) {
  37.                         Address address = (Address) addressList.get(0);
  38.                         StringBuilder sb = new StringBuilder();
  39.                         sb.append(address.getLatitude()).append("\n");
  40.                         sb.append(address.getLongitude()).append("\n");
  41.                         callback.getLatLng(address.getLatitude(), address.getLongitude());
  42.  
  43.  
  44.  
  45.                     }
  46.                 } catch (IOException e) {
  47.                     Log.e(TAG, "Unable to connect to Geocoder", e);
  48.                     callback.ErrorMessage(e.getLocalizedMessage());
  49.                 } finally {
  50.  
  51.                 }
  52.             }
  53.         };
  54.         thread.start();
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement