从getAdminArea()获得状态缩写. [英] Obtaining State abbreviation from getAdminArea();

查看:56
本文介绍了从getAdminArea()获得状态缩写.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've attempted two different ways in trying to obtain the city name as well as the state abbreviation only from the Address class, with no luck. The first is returning the State like so "CA 92055" with the zip code, and the second attempt returns the full State name. Any quick ways around this?

该州最后一次尝试返回"CA 92055"(缩写后是邮政编码)

First attempt which the state ends up returning "CA 92055" (Zip followed after the abbrev)

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();
                }

第二次尝试,现在越来越近没有zip ...但是...(返回全州名称):

Second attempt, getting closer no zip now...but... (Returns the full state name):

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()获得状态缩写.但是,在与美国和加拿大打交道时,您可以设置一个哈希图,以将州/省映射为参考的缩写.

I don't believe you can get a state abbreviation directly from getAdminArea() despite what the documentation says. However, when dealing with US and Canada, you can set up a hashmap that will map out the state/provinces to the abbreviations on reference.

使用类似这样的内容:

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");

这篇关于从getAdminArea()获得状态缩写.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆