How to get addresses if we give latitude and longitude using location API in Android
-
01-10-2019 - |
Question
I have an application where I try to get address of a location based on the latitude,longitude coordinates.When I try to print all the address of particular coordinates , I am getting only single address. IS there any way to get the list of all addresses for the coordinates supplied . My code snippet is as below:
Where locationLatitude,locationLongitude are of type String which are converted to doubles. Address is a class which has getters and setters methods for getting details of state,city,country etc.
List<Address> addresses = geoCoder.getFromLocation(Double.parseDouble(locationLatitude),
Double.parseDouble(locationLongitude), 1);
StringBuffer addressAsString = new StringBuffer("");
if(!addresses.isEmpty()){
for (int i = 0; i < addresses.get(0).getMaxAddressLineIndex(); i++)
addressAsString.append(addresses.get(0).getAddressLine(i) + "\n");
return addressAsString.toString();
}
Solution
Loop through all your addresses,and change the third parameterof getFromLocation to 10 instead of 1 to fetch all 10 surrounding locations at the given geo point:
List addresses = geoCoder.getFromLocation(Double .parseDouble(locationLatitude), Double.parseDouble(locationLongitude), 10);
for (Address addr:List addresses) {
for (int i = 0; i < addr.getMaxAddressLineIndex(); i++) {
// your previous code here
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow