Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save KhaledAlharthi/6f18710ba3ea6036c28fb5a5a84e7c33 to your computer and use it in GitHub Desktop.
Save KhaledAlharthi/6f18710ba3ea6036c28fb5a5a84e7c33 to your computer and use it in GitHub Desktop.
/*
* This functions is used to retrieve neighborhood, and regoin from known (lat, lng) coordinates
* the returned pattern: حي قرطبة، الرياض or الرياض if neighborhood is unknown
*
*/
public static String getAddress(Context context, double LATITUDE, double LONGITUDE) {
StringBuilder strAdd = new StringBuilder();
Geocoder geocoder = new Geocoder(context, Locale.forLanguageTag("ar"));
try {
List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);
if (addresses != null) {
Address returnedAddress = addresses.get(0);
String adminArea = returnedAddress.getAdminArea();
String city = returnedAddress.getLocality();
String neighborhood = returnedAddress.getThoroughfare();
String subLocality = returnedAddress.getSubLocality();
if (city != null){
strAdd.append(city);
}else if (adminArea!=null){
strAdd.append(adminArea);
}
if (subLocality!=null){
strAdd.append("، ");
strAdd.append(subLocality);
}else if (neighborhood!=null){
strAdd.append("، ");
strAdd.append(neighborhood);
}
} else {
Log.w("LocationAddress", "No address returned");
}
} catch (Exception e) {
e.printStackTrace();
Log.w("CurrentLocation", "Canont get address");
}
return strAdd.toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment