Created
December 30, 2016 11:54
-
-
Save ashmeh6/4dfddbc69844ef51b68beb602866a3aa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private class FetchAddressFromLocation extends AsyncTask<Void, Void, String> { | |
private Context context; | |
private Double latitude; | |
private Double longitude; | |
public FetchAddressFromLocation(Context context, Double latitude, Double longitude) { | |
this.context = context; | |
this.latitude = latitude; | |
this.longitude = longitude; | |
} | |
@Override | |
protected String doInBackground(Void... voids) { | |
Geocoder geocoder = new Geocoder(context, Locale.getDefault()); | |
String result = null; | |
try { | |
List<Address> addressList = geocoder.getFromLocation( | |
latitude, longitude, 1); | |
if (addressList != null && addressList.size() > 0) { | |
Address address = addressList.get(0); | |
StringBuilder sb = new StringBuilder(); | |
for (int i = 0; i < address.getMaxAddressLineIndex(); i++) { | |
sb.append(address.getAddressLine(i)).append("\n"); | |
} | |
sb.append(address.getLocality()).append("\n"); | |
sb.append(address.getPostalCode()).append("\n"); | |
sb.append(address.getCountryName()); | |
result = sb.toString(); | |
} | |
} catch (IOException e) { | |
Log.e("TAG", "Unable connect to Geocoder", e); | |
} finally { | |
if (result != null) { | |
Bundle bundle = new Bundle(); | |
result = "Latitude: " + latitude + " Longitude: " + longitude + | |
"\n\nAddress:\n" + result; | |
bundle.putString("address", result); | |
} else { | |
Bundle bundle = new Bundle(); | |
result = "Latitude: " + latitude + " Longitude: " + longitude + | |
"\n Unable to get address for this lat-long."; | |
bundle.putString("address", result); | |
} | |
} | |
return result; | |
} | |
@Override | |
protected void onPostExecute(String s) { | |
AlertDialog.Builder builder = new AlertDialog.Builder(context); | |
builder.setMessage(s); | |
builder.create().show(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment