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

后端 未结 2 1331
走了就别回头了
走了就别回头了 2021-01-24 03:44

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

相关标签:
2条回答
  • 2021-01-24 04:05

    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/

    0 讨论(0)
  • 2021-01-24 04:15

    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.

    0 讨论(0)
提交回复
热议问题