从getAdminArea()获取State缩写;

Joe*_*oey 11 android reverse-geocoding

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

国家最终返回"CA 92055"的第一次尝试(Zip后面的缩写)

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();
                }
Run Code Online (Sandbox Code Playgroud)

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

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();
                }
Run Code Online (Sandbox Code Playgroud)

Bou*_*rne 8

我不相信你可以直接从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");
Run Code Online (Sandbox Code Playgroud)

  • 我在这个解决方案中看到的问题是除英语之外的其他语言的州名. (2认同)

pel*_*lfo 5

我通过查找完整地址中的最后2个字母单词来解决这个问题(假设google maps android地理编码器提供了美国地址).它适用于我发现的所有情况:

private String getUSStateCode(Address USAddress){
    String fullAddress = "";
    for(int j = 0; j <= USAddress.getMaxAddressLineIndex(); j++)
        if (USAddress.getAddressLine(j) != null)
            fullAddress = fullAddress + " " + USAddress.getAddressLine(j);

    String stateCode = null;
    Pattern pattern = Pattern.compile(" [A-Z]{2} ");
    String helper = fullAddress.toUpperCase().substring(0, fullAddress.toUpperCase().indexOf("USA"));
    Matcher matcher = pattern.matcher(helper);
    while (matcher.find())
        stateCode = matcher.group().trim();

    return stateCode;
}
Run Code Online (Sandbox Code Playgroud)