从getAdminArea()获取State缩写;

时间:2014-11-12 05:27:20

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

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

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

4 个答案:

答案 0 :(得分: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");

答案 1 :(得分: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;
}

答案 2 :(得分:0)

这是Bourne和pellyadolfo答案的组合和修改版本。它首先尝试将完整的州名称映射到州代码(也适用于加拿大省份,并且比正则表达式更不容易出错),如果这不起作用,那么它将依赖于正则表达式解决方案(有可能错误,因此我更喜欢它作为备份解决方案,但可以使用不同的语言或国家)。

正则表达式解决方案已经改进,包括在开始时进行健全性检查,并且具有更高级的正则表达式,以消除需要手动过滤掉“USA”(这允许加拿大地址工作)。它还删除了“toupper()”调用,该调用具有将“St”(“street”的缩写)转换为“ST”的副作用,这可能导致错误匹配。

import android.location.Address;
import android.util.Log;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StateNameAbbreviator {
    private static final String TAG = "StateNameAbbreviator";

    static private Map<String, String> mStateMap = null;

    static public String getStateAbbreviation(Address address) {
        if (address == null) {
            return null;
        }

        populateStates();

        String stateCode = mStateMap.get(address.getAdminArea());
        if (stateCode == null) {
            Log.d(TAG, "State mapping failed, parsing from address");
            stateCode = parseStateCodeFromFullAddress(address);
            if (stateCode == null) {
                Log.d(TAG, "Could not parse state from address");
            }
        }
        else {
            Log.d(TAG, "Successfully mapped " + address.getAdminArea() + " to " + stateCode);
        }

        return stateCode;
    }

    static private String parseStateCodeFromFullAddress(Address address) {
        if ((address == null) || address.getMaxAddressLineIndex() < 0) {
            return null;
        }

        String fullAddress = "";
        for(int j = 0; j <= address.getMaxAddressLineIndex(); j++) {
            if (address.getAddressLine(j) != null) {
                fullAddress += " " + address.getAddressLine(j);
            }
        }

        Log.d(TAG, "Full address: " + fullAddress);

        Pattern pattern = Pattern.compile("(?<![A-Za-z0-9])([A-Z]{2})(?![A-Za-z0-9])");
        Matcher matcher = pattern.matcher(fullAddress);

        String stateCode = null;
        while (matcher.find()) {
            stateCode = matcher.group().trim();
        }

        Log.d(TAG, "Parsed statecode: " + stateCode);

        return stateCode;
    }

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

正则表达式将匹配任何两个字母的大写字:

(?<![A-Za-z0-9])([A-Z]{2})(?![A-Za-z0-9])

这里的挑战是“USA”实际上将“US”与简单的双字母大写搜索匹配。所以我们需要一个前瞻和后视:

?<!

看比赛背后

(?<![A-Za-z0-9])

查看比赛背后,并确保那里没有字母数字字符(即,在比赛前必须是“行首”,空格,逗号等)

([A-Z]{2})

匹配两个大写字母

?!

比赛前瞻

(?![A-Za-z0-9])

向前看比赛,并确保那里没有字母数字字符(即,在比赛结束后必须是“行尾”或空格,逗号等)

答案 3 :(得分:-2)

This works for me on US addresses:

String[] spState = addressInformation.get(0).getAddressLine(1).split(" ");
String state = spState[1];