其他分享
首页 > 其他分享> > android-从getAdminArea()获得状态缩写

android-从getAdminArea()获得状态缩写

作者:互联网

我尝试了两种不同的方法来尝试仅从Address类获得城市名称以及州缩写,但是没有运气.第一种是使用邮政编码返回“ CA 92055”之类的州,而第二种尝试将返回完整的州名称.有什么快速的方法吗?

状态最终返回“ CA 92055”的首次尝试(缩写后是邮政编码)

Geocoder geoCoder = new Geocoder(getActivity(), Locale.getDefault());
                List<Address> addresses;
                try {
                    addresses = geoCoder.getFromLocation(mLatitude, mLongitude, 10);
                     int i=1;
                     for(Address addObj:addresses)
                     {
                         // Looping once
                         if(i==1)
                         {

                             String add_line1_extract;

                             add_line1_extract=addObj.getAddressLine(1);

                             String string = add_line1_extract;
                             String[] parts = string.split(",");

                             //Setting city
                             mCity = parts[0]; 

                             //setting state
                             mState = parts[1]; 

                             // Final Output
                             String cityAndState = mCity + ", " + mState;
                             i++;

                         }
                     }
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

第二次尝试,现在越来越近没有拉链…但是…(返回完整的州名):

Geocoder geoCoder = new Geocoder(getActivity(), Locale.getDefault());
                List<Address> addresses;
                try {
                    addresses = geoCoder.getFromLocation(mLatitude, mLongitude, 10);
                     int i=1;
                     for(Address addObj:addresses)
                     {
                         // Looping once
                         if(i==1)
                         {

                             //Setting city
                             mCity = addObj.getSubLocality();                            
                             //setting state
                             mState = addObj.getAdminArea(); 

                             i++;
                         }
                     }
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

解决方法:

尽管文档说了什么,但我不相信您可以直接从getAdminArea()获得状态缩写.但是,在与美国和加拿大打交道时,您可以设置一个哈希图,以将州/省映射为参考的缩写.

使用这样的东西:

Map<String, String> states = new HashMap<String, String>();
states.put("Alabama","AL");
states.put("Alaska","AK");
states.put("Alberta","AB");
states.put("American Samoa","AS");
states.put("Arizona","AZ");
states.put("Arkansas","AR");
states.put("Armed Forces (AE)","AE");
states.put("Armed Forces Americas","AA");
states.put("Armed Forces Pacific","AP");
states.put("British Columbia","BC");
states.put("California","CA");
states.put("Colorado","CO");
states.put("Connecticut","CT");
states.put("Delaware","DE");
states.put("District Of Columbia","DC");
states.put("Florida","FL");
states.put("Georgia","GA");
states.put("Guam","GU");
states.put("Hawaii","HI");
states.put("Idaho","ID");
states.put("Illinois","IL");
states.put("Indiana","IN");
states.put("Iowa","IA");
states.put("Kansas","KS");
states.put("Kentucky","KY");
states.put("Louisiana","LA");
states.put("Maine","ME");
states.put("Manitoba","MB");
states.put("Maryland","MD");
states.put("Massachusetts","MA");
states.put("Michigan","MI");
states.put("Minnesota","MN");
states.put("Mississippi","MS");
states.put("Missouri","MO");
states.put("Montana","MT");
states.put("Nebraska","NE");
states.put("Nevada","NV");
states.put("New Brunswick","NB");
states.put("New Hampshire","NH");
states.put("New Jersey","NJ");
states.put("New Mexico","NM");
states.put("New York","NY");
states.put("Newfoundland","NF");
states.put("North Carolina","NC");
states.put("North Dakota","ND");
states.put("Northwest Territories","NT");
states.put("Nova Scotia","NS");
states.put("Nunavut","NU");
states.put("Ohio","OH");
states.put("Oklahoma","OK");
states.put("Ontario","ON");
states.put("Oregon","OR");
states.put("Pennsylvania","PA");
states.put("Prince Edward Island","PE");
states.put("Puerto Rico","PR");
states.put("Quebec","PQ");
states.put("Rhode Island","RI");
states.put("Saskatchewan","SK");
states.put("South Carolina","SC");
states.put("South Dakota","SD");
states.put("Tennessee","TN");
states.put("Texas","TX");
states.put("Utah","UT");
states.put("Vermont","VT");
states.put("Virgin Islands","VI");
states.put("Virginia","VA");
states.put("Washington","WA");
states.put("West Virginia","WV");
states.put("Wisconsin","WI");
states.put("Wyoming","WY");
states.put("Yukon Territory","YT");

标签:reverse-geocoding,android
来源: https://codeday.me/bug/20191010/1887126.html