I am trying to figure out exactly how to get address component with the Android SDK with the class android.location.Address.
Some of the methods are very straightforward, other are easily understood with the examples in the documentation, but some of them are completely unclear to me. Either because there's no example in the documentation or because the example refers to the US, which does not have the same political organization as other countries may have.
I've been looking for their meaning, but most of tutorials on the web simply use the method getAddressLine(int index)
to teach and then it is up to the developer to learn the rest.
getAdminArea()
: returns the state acronym ("CA", for California)getCountryCode()
: returns the country ISO code ("JP", for Japan)getCountryName()
: returns country name ("Spain", for... Spain)getFeatureName()
: returns the name of the location, if any ("Louvre", for the museum)getLocality()
: returns the city name ("London")getPostalCode()
: returns the postal code ("94110", in the US)getPremises()
: ???getSubAdminArea()
: ???getSubLocality()
: ???getSubThoroughfare()
: ???getThoroughfare()
: returns the street and building number ("1600 Amphitheater Parkway")My question is what all of these methods return (and examples, if possible).
Also, I'd like to know how to get the building number and street name separately. Parsing the Thoroughfare string does not seem to be that hard, but taking account that in some countries the number comes before the street, while other comes after, what is the best way to parse the text?
I did not find complete documentation on the standard that android.location.Address
uses to identify and store the address globally, so I made several address queries from different countries in order to interpret the results.
As Guilherme says, we have getAddressLine(int index)
and a list of methods to extract every element of an address.
This post is not intended to explain the code, but I put it here for those who need to see it.
List<Address> addresses;
Geocoder geocoder = new Geocoder(getActivity());
addresses = geocoder.getFromLocation(latitude, longitude, 10);
if (addresses == null || addresses.isEmpty()) {
// Mygeocoder is a class with a http request to google server, that replaces Geocoder, if not work
addresses = MyGeocoder.getFromLocation(latitude, longitude, 10);
}
HashMap itemAddress;
ArrayList itemList = new ArrayList<HashMap<String, String>>();
Log.d("Addresses", "" + "Start to print the ArrayList");
for (int i = 0; i < addresses.size(); i++) {
itemAddress = new HashMap<String, String>();
Address address = addresses.get(i);
String addressline = "Addresses from getAddressLine(): ";
for (int n = 0; n <= address.getMaxAddressLineIndex(); n++) {
addressline += " index n: " + n + ": " + address.getAddressLine(n) + ", ";
}
Log.d("Addresses: ", addressline);
Log.d("Addresses getAdminArea()", "" + address.getAdminArea());
Log.d("Addresses getCountryCode()", "" + address.getCountryCode());
Log.d("Addresses getCountryName()", "" + address.getCountryName());
Log.d("Addresses getFeatureName()", "" + address.getFeatureName());
Log.d("Addresses getLocality()", "" + address.getLocality());
Log.d("Addresses getPostalCode()", "" + address.getPostalCode());
Log.d("Addresses getPremises()", "" + address.getPremises());
Log.d("Addresses getSubAdminArea()", "" + address.getSubAdminArea());
Log.d("Addresses getSubLocality()", "" + address.getSubLocality());
Log.d("Addresses getSubThoroughfare()", "" + address.getSubThoroughfare());
Log.d("Addresses getThoroughfare()", "" + address.getThoroughfare());
}
Here are the results from a point with latitude and longitude in Miami:
In the above sets of results, we see that yielded 6 sets of results Geocoder, if we analyze are layered location from the exact address with all its attributes, so only the country. It is as if each layer was a different map, a map of single countries, map of states and countries, until the map for directions and streets, and every set of Geocoder query results from each map.
I consulted a point in a village in Colombia, Geocoder gave me 5 sets of results. Let's analyze only the first set to compare with the previous one.
The only difference is that these results have no PostalCode and SubAdminArea.
Now, I consulted a point in Egypt:
Egypt has many differences; for example, getMaxAddressLine()
throws 5 results, Colombia and the United States only 3 results. Changing the order of the country's location in getAddressLine()
in Colombia and USA is in getAddressLine(2)
Egypt is getAddressLine(4)
. getLocality()
should have the city "Cairo", but is not saved.
In conclusion, Geocoder results list is an adaptation for the system in each country, and the update is missing.
Already depends on application being developed, to optimize results shown Geocoder, if we show the user where it is located, it is best to use getAddressLine()
, but if we need the country to a database, you must use getCountry()
. If you want more details, we must identify the system of each country or region and develop for each.