本文介绍了每个Android的location.Address方法返回什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过类android.location.Address来确切地找到如何使用Android SDK获取地址组件.

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.

我一直在寻找它们的含义,但是网络上的大多数教程都只是使用方法 getAddressLine(int index)进行教学,然后由开发人员来学习其余内容.

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():返回州首字母缩写("CA",代表加利福尼亚)
  • getCountryCode():返回国家/地区ISO代码("JP",对于日本)
  • getCountryName():返回国家/地区名称(西班牙",用于...西班牙)
  • getFeatureName():返回位置名称(如果有的话)(博物馆为"Louvre")
  • getLocality():返回城市名称(伦敦")
  • getPostalCode():返回邮政编码(美国为"94110")
  • getPremises():???
  • getSubAdminArea():???
  • getSubLocality():???
  • getSubThoroughfare():???
  • getThoroughfare():返回街道和建筑物编号("1600 Amphitheatre Parkway")
  • 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).

此外,我想知道如何分别获取建筑物编号和街道名称.解析Thorffare字符串似乎并不那么困难,但是考虑到在某些国家/地区中,数字先出现在街道上,而在其他国家/地区出现在后面,那么解析文本的最佳方法是什么?

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?

推荐答案

我没有找到有关 android.location.Address 用于全局标识和存储地址的标准的完整文档,所以我为了解释结果,对来自不同国家的几个地址进行了查询.

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.

正如Guilherme所说,我们有 getAddressLine(int index)和一系列提取地址中每个元素的方法.

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/地址:开始打印ArrayList
  • D/地址:﹕来自getAddressLine()的地址:索引n:0:7500 SW 120th St,索引n:1:佛罗里达州迈阿密,33156,索引n:2:EE.UU.,
  • D/地址getAdminArea():佛罗里达州
  • D/地址getCountryCode():美国
  • D/地址getCountryName():Estados Unidos
  • D/地址getFeatureName():7500
  • D/地址getLocality():迈阿密
  • D/地址getPostalCode():33156
  • D/地址getPremises():空
  • D/地址getSubAdminArea():空
  • D/地址getSubLocality():空
  • D/地址getSubThoroughfare():7500
  • D/地址getThoroughfare():SW 120th St
  • D/地址:﹕来自getAddressLine()的地址:索引n:0:佛罗里达派恩克雷斯特,索引n:1:EE.UU.,
  • D/地址getAdminArea():佛罗里达州
  • D/地址getCountryCode():美国
  • D/地址getCountryName():Estados Unidos
  • D/地址getFeatureName():Pinecrest
  • D/地址getLocality():Pinecrest
  • D/地址getPostalCode():空
  • D/地址getPremises():空
  • D/地址getSubAdminArea():迈阿密戴德大街
  • D/地址getSubLocality():空
  • D/地址getSubThoroughfare():空
  • D/地址getThoroughfare():空
  • D/地址:﹕来自getAddressLine()的地址:索引n:0:佛罗里达州迈阿密33156,索引n:1:EE.UU.,
  • D/地址getAdminArea():佛罗里达州
  • D/地址getCountryCode():美国
  • D/地址getCountryName():Estados Unidos
  • D/地址getFeatureName():33156
  • D/地址getLocality():迈阿密
  • D/地址getPostalCode():33156
  • D/地址getPremises():空
  • D/地址getSubAdminArea():空
  • D/地址getSubLocality():空
  • D/地址getSubThoroughfare():空
  • D/地址getThoroughfare():空
  • D/地址:﹕来自getAddressLine()的地址:索引n:0:迈阿密达德大道,索引n:1:佛罗里达,索引n:2:EE.UU.,
  • D/地址getAdminArea():佛罗里达州
  • D/地址getCountryCode():美国
  • D/地址getCountryName():Estados Unidos
  • D/地址getFeatureName():迈阿密戴德大道(Condado de Miami-Dade)
  • D/地址getLocality():空
  • D/地址getPostalCode():空
  • D/地址getPremises():空
  • D/地址getSubAdminArea():迈阿密戴德大街
  • D/地址getSubLocality():空
  • D/地址getSubThoroughfare():空
  • D/地址getThoroughfare():空
  • D/地址:﹕来自getAddressLine()的地址:索引n:0:佛罗里达,索引n:1:EE.UU.,
  • D/地址getAdminArea():佛罗里达州
  • D/地址getCountryCode():美国
  • D/地址getCountryName():Estados Unidos
  • D/地址getFeatureName():佛罗里达
  • D/地址getLocality():空
  • D/地址getPostalCode():空
  • D/地址getPremises():空
  • D/地址getSubAdminArea():空
  • D/地址getSubLocality():空
  • D/地址getSubThoroughfare():空
  • D/地址getThoroughfare():空
  • D/地址:﹕ getAddressLine()中的地址:索引n:0:Estados Unidos,
  • D/地址getAdminArea():空
  • D/地址getCountryCode():美国
  • D/地址getCountryName():Estados Unidos
  • D/地址getFeatureName():Estados Unidos
  • D/地址getLocality():空
  • D/地址getPostalCode():空
  • D/地址getPremises():空
  • D/地址getSubAdminArea():空
  • D/地址getSubLocality():空
  • D/地址getSubThoroughfare():空
  • D/地址getThoroughfare():空

在上面的结果集中,我们看到产生了6组结果Geocoder,如果我们从具有其所有属性的确切地址开始的分层位置分析,则只有国家/地区.好像每一层都是一张不同的地图,一个国家的地图,州和国家的地图,直到方向和街道的地图,以及每个地图的每组Geocoder查询结果.

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.

我咨询了哥伦比亚一个村庄的一个地点,Geocoder给了我5组结果.让我们仅分析第一组与上一组进行比较.

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/地址:开始打印ArrayList
  • D/地址:﹕来自getAddressLine()的地址:索引n:0:Calle 34#36-2 a 36-100,索引n:1:Palmira,Valle del Cauca,索引n:2:哥伦比亚,
  • D/地址getAdminArea():Valle del Cauca
  • D/地址getCountryCode():CO
  • D/地址getCountryName():哥伦比亚
  • D/地址getFeatureName():362-36100
  • D/地址getLocality():Palmira
  • D/地址getPostalCode():空
  • D/地址getPremises():空
  • D/地址getSubAdminArea():空
  • D/地址getSubLocality():空
  • D/地址getSubThoroughfare():362-36100
  • D/地址getThoroughfare():Calle 34

唯一的区别是这些结果没有PostalCode和SubAdminArea.

The only difference is that these results have no PostalCode and SubAdminArea.

现在,我咨询了埃及的一个问题:

Now, I consulted a point in Egypt:

  • D/地址:开始打印ArrayList
  • D/地址:﹕来自getAddressLine()的地址:索引n:0:حارةعابدين,索引n:1:الزيتونالبحرية,索引n:2:الزيتون,索引n:3:Gobernaciónde El Cairo,索引n:4:Egipto,
  • D/地址getAdminArea():空
  • D/地址getCountryCode():EG
  • D/地址getCountryName():Egipto
  • D/地址getFeatureName():حارةعابدين
  • D/地址getLocality():空
  • D/地址getPostalCode():空
  • D/地址getPremises():空
  • D/地址getSubAdminArea():空
  • D/地址getSubLocality():空
  • D/地址getSubThoroughfare():空
  • D/地址getThoroughfare():حارةعابدين

埃及有许多不同之处;例如, getMaxAddressLine()会抛出5个结果,而哥伦比亚和美国则只有3个结果.更改国家/地区在哥伦比亚和美国的 getAddressLine()中的位置顺序是在 getAddressLine(2)中,埃及是 getAddressLine(4). getLocality()应该具有城市开罗",但未保存.

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.

最后,Geocoder结果列表是针对每个国家/地区的系统的改编版,并且缺少更新.

In conclusion, Geocoder results list is an adaptation for the system in each country, and the update is missing.

已经取决于正在开发的应用程序,为了优化显示在Geocoder中的结果,如果我们向用户显示它所在的位置,最好使用 getAddressLine(),但是如果需要将国家/地区作为数据库,您必须使用 getCountry().如果您需要更多详细信息,我们必须确定每个国家或地区的系统并针对每个系统或系统进行开发.

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.

这篇关于每个Android的location.Address方法返回什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 06:25