Skip to content

Instantly share code, notes, and snippets.

@DanishAmjad12
Created July 23, 2018 06:54
Show Gist options
  • Save DanishAmjad12/d21e5c3bdbba344f50e272d3fe4a48d8 to your computer and use it in GitHub Desktop.
Save DanishAmjad12/d21e5c3bdbba344f50e272d3fe4a48d8 to your computer and use it in GitHub Desktop.
private void onLocationReceived() {
String location = getLocationName(mLocationModel.getLat(), mLocationModel.getLng());
if(!TextUtils.isEmpty(location))
//your work
}
private String getLocationName(double lattitude, double longitude) {
String locationStr = "";
Geocoder gcd = new Geocoder(getActivity(), Locale.getDefault());
try {
List<Address> addresses = gcd.getFromLocation(lattitude, longitude, 5);
for (Address adrs : addresses) {
if (adrs != null) {
city = (adrs.getLocality() == null) ? "" : adrs.getLocality();
country = adrs.getCountryName();
state = (adrs.getAdminArea() == null) ? "" : adrs.getAdminArea();
postalcode = adrs.getPostalCode();
StringBuilder sb = new StringBuilder();
sb.append(city);
if (!city.isEmpty() && !state.isEmpty())
sb.append(", ");
sb.append(state);
locationStr = sb.toString();
if (!city.isEmpty() && !state.isEmpty()) {
break;
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
return locationStr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment