Last active
July 23, 2018 06:49
-
-
Save DanishAmjad12/77fe4f3b2257eb4039ab40613b6a71ce to your computer and use it in GitHub Desktop.
This file contains hidden or 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 void onLocationReceived() { | |
String location = getLocationName(mLocationModel.getLat(), mLocationModel.getLng()); | |
if(location!=null && !location.isEmpty()) | |
//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