Android: Does Address(from GeoCode) have fixed format?

不羁岁月 提交于 2019-12-02 11:08:01

问题


I need to determine the address for a geo location, so I used GeoCoder and Address, I tried to print an Address object and got the following:

(for privacy reason, I used some fake data, but the data I received on my device is real)

Address[addressLines=[0:"123 ABC St",1:"Melbourne VIC 1234",2:"Australia"],feature=123,admin=Victoria,sub-admin=null,locality=Melbourne,thoroughfare=ABC St,postalCode=1234,countryCode=AU,countryName=Australia,hasLatitude=true,latitude=-123.321,hasLongitude=true,longitude=123.321,phone=null,url=null,extras=null]

Now I'm wondering, can I assume that:

  1. address line index 0 is always the street address?
  2. and index 1 and to is the suburb, state, postcode pair, and index 2 is the country?
  3. "feature" is actually the street number(ie 123)?
  4. "admin" is state?

If the format is NOT fixed, how do I get address information? Thanks!


回答1:


I don't think that the format is fixed and you should not rely on the array indexes. From the docs:

The amount of detail in a reverse geocoded location description may vary, for example one might contain the full street address of the closest building, while another might contain only a city name and postal code. The Geocoder class requires a backend service that is not included in the core android framework.

The geocoding service is implemented by the Google Maps service which runs in the backgroind I believe. You should use Address class to fetch the elements of the address that you require.




回答2:


Here is a snippet that may help.

-> using GeoCoder

    String currentAddress = "";

    try {
        Geocoder geoCoder = new Geocoder(context);
        List<Address> adds = geoCoder.getFromLocation(
                        latitude
                        , longitude
                        , 1);

        if (adds!=null && adds.size()>0) {
             Address add = adds.get(0);
             int max = add.getMaxAddressLineIndex();
             if (max!=-1) {
                   for (int i=0; i<max;i++)
                   currentAddress += add.getAddressLine(i) + " ";
             }
        }
}
catch (Exception ex) {
     Log.w(TAG, "geocoding_fromAndroid->"+ex.toString());
     currentAddress = "";
}


Please keep in mind, GeoCoder is NOT implemented in all Android devices :| in this case, you can use Google API directly

-> http://code.google.com/apis/maps/documentation/geocoding/



来源:https://stackoverflow.com/questions/6433922/android-does-addressfrom-geocode-have-fixed-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!