Created
July 23, 2018 06:54
-
-
Save DanishAmjad12/d21e5c3bdbba344f50e272d3fe4a48d8 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(!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