问题
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?
回答1:
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:
- D/Addresses﹕ Start to print the ArrayList
- D/Addresses:﹕ Addresses from getAddressLine(): index n: 0: 7500 SW 120th St, index n: 1: Miami, Florida 33156, index n: 2: EE. UU.,
- D/Addresses getAdminArea()﹕ Florida
- D/Addresses getCountryCode()﹕ US
- D/Addresses getCountryName()﹕ Estados Unidos
- D/Addresses getFeatureName()﹕ 7500
- D/Addresses getLocality()﹕ Miami
- D/Addresses getPostalCode()﹕ 33156
- D/Addresses getPremises()﹕ null
- D/Addresses getSubAdminArea()﹕ null
- D/Addresses getSubLocality()﹕ null
- D/Addresses getSubThoroughfare()﹕ 7500
- D/Addresses getThoroughfare()﹕ SW 120th St
- D/Addresses:﹕ Addresses from getAddressLine(): index n: 0: Pinecrest, Florida, index n: 1: EE. UU.,
- D/Addresses getAdminArea()﹕ Florida
- D/Addresses getCountryCode()﹕ US
- D/Addresses getCountryName()﹕ Estados Unidos
- D/Addresses getFeatureName()﹕ Pinecrest
- D/Addresses getLocality()﹕ Pinecrest
- D/Addresses getPostalCode()﹕ null
- D/Addresses getPremises()﹕ null
- D/Addresses getSubAdminArea()﹕ Condado de Miami-Dade
- D/Addresses getSubLocality()﹕ null
- D/Addresses getSubThoroughfare()﹕ null
- D/Addresses getThoroughfare()﹕ null
- D/Addresses:﹕ Addresses from getAddressLine(): index n: 0: Miami, Florida 33156, index n: 1: EE. UU.,
- D/Addresses getAdminArea()﹕ Florida
- D/Addresses getCountryCode()﹕ US
- D/Addresses getCountryName()﹕ Estados Unidos
- D/Addresses getFeatureName()﹕ 33156
- D/Addresses getLocality()﹕ Miami
- D/Addresses getPostalCode()﹕ 33156
- D/Addresses getPremises()﹕ null
- D/Addresses getSubAdminArea()﹕ null
- D/Addresses getSubLocality()﹕ null
- D/Addresses getSubThoroughfare()﹕ null
- D/Addresses getThoroughfare()﹕ null
- D/Addresses:﹕ Addresses from getAddressLine(): index n: 0: Condado de Miami-Dade, index n: 1: Florida, index n: 2: EE. UU.,
- D/Addresses getAdminArea()﹕ Florida
- D/Addresses getCountryCode()﹕ US
- D/Addresses getCountryName()﹕ Estados Unidos
- D/Addresses getFeatureName()﹕ Condado de Miami-Dade
- D/Addresses getLocality()﹕ null
- D/Addresses getPostalCode()﹕ null
- D/Addresses getPremises()﹕ null
- D/Addresses getSubAdminArea()﹕ Condado de Miami-Dade
- D/Addresses getSubLocality()﹕ null
- D/Addresses getSubThoroughfare()﹕ null
- D/Addresses getThoroughfare()﹕ null
- D/Addresses:﹕ Addresses from getAddressLine(): index n: 0: Florida, index n: 1: EE. UU.,
- D/Addresses getAdminArea()﹕ Florida
- D/Addresses getCountryCode()﹕ US
- D/Addresses getCountryName()﹕ Estados Unidos
- D/Addresses getFeatureName()﹕ Florida
- D/Addresses getLocality()﹕ null
- D/Addresses getPostalCode()﹕ null
- D/Addresses getPremises()﹕ null
- D/Addresses getSubAdminArea()﹕ null
- D/Addresses getSubLocality()﹕ null
- D/Addresses getSubThoroughfare()﹕ null
- D/Addresses getThoroughfare()﹕ null
- D/Addresses:﹕ Addresses from getAddressLine(): index n: 0: Estados Unidos,
- D/Addresses getAdminArea()﹕ null
- D/Addresses getCountryCode()﹕ US
- D/Addresses getCountryName()﹕ Estados Unidos
- D/Addresses getFeatureName()﹕ Estados Unidos
- D/Addresses getLocality()﹕ null
- D/Addresses getPostalCode()﹕ null
- D/Addresses getPremises()﹕ null
- D/Addresses getSubAdminArea()﹕ null
- D/Addresses getSubLocality()﹕ null
- D/Addresses getSubThoroughfare()﹕ null
- D/Addresses getThoroughfare()﹕ null
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.
- D/Addresses﹕ Start to print the ArrayList
- D/Addresses:﹕ Addresses from getAddressLine(): index n: 0: Calle 34 # 36-2 a 36-100, index n: 1: Palmira, Valle del Cauca, index n: 2: Colombia,
- D/Addresses getAdminArea()﹕ Valle del Cauca
- D/Addresses getCountryCode()﹕ CO
- D/Addresses getCountryName()﹕ Colombia
- D/Addresses getFeatureName()﹕ 362-36100
- D/Addresses getLocality()﹕ Palmira
- D/Addresses getPostalCode()﹕ null
- D/Addresses getPremises()﹕ null
- D/Addresses getSubAdminArea()﹕ null
- D/Addresses getSubLocality()﹕ null
- D/Addresses getSubThoroughfare()﹕ 362-36100
- D/Addresses getThoroughfare()﹕ Calle 34
The only difference is that these results have no PostalCode and SubAdminArea.
Now, I consulted a point in Egypt:
- D/Addresses﹕ Start to print the ArrayList
- D/Addresses:﹕ Addresses from getAddressLine(): index n: 0: حارة عابدين, index n: 1: الزيتون البحرية, index n: 2: الزيتون, index n: 3: Gobernación de El Cairo, index n: 4: Egipto,
- D/Addresses getAdminArea()﹕ null
- D/Addresses getCountryCode()﹕ EG
- D/Addresses getCountryName()﹕ Egipto
- D/Addresses getFeatureName()﹕ حارة عابدين
- D/Addresses getLocality()﹕ null
- D/Addresses getPostalCode()﹕ null
- D/Addresses getPremises()﹕ null
- D/Addresses getSubAdminArea()﹕ null
- D/Addresses getSubLocality()﹕ null
- D/Addresses getSubThoroughfare()﹕ null
- D/Addresses getThoroughfare()﹕ حارة عابدين
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.
来源:https://stackoverflow.com/questions/22096011/what-does-each-androids-location-address-method-return