Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtaining State abbreviation from getAdminArea();

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?

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

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();
                }
like image 889
Joey Avatar asked Nov 12 '14 05:11

Joey


People also ask

How do you abbreviate a state's name?

Most traditional abbreviations use the first three, four or five letters of the state's name, such as Colorado (Colo.) or Nevada (Nev.) However, there are a few states that break the rules or are especially confusing. Some of these states of confusion include: States with five or fewer letters are never abbreviated in traditional abbreviations.

What are the 10 traditional state abbreviations?

Traditional State Abbreviations 1 Alabama - Ala. 2 Alaska - Alaska 3 Arizona - Ariz. 4 Arkansas - Ark. 5 California - Calif. 6 Colorado - Colo. 7 Connecticut - Conn. 8 Delaware - Del. 9 District of Columbia - D.C. 10 Florida - Fla. More items...

What is the US state abbreviation for Georgia?

US State Abbreviations List State Name USPS Abbreviation Traditional Abbreviation Georgia GA Ga. Hawaii HI Hawaii Idaho ID Idaho Illinois IL Ill. 46 more rows ...

What is the abbreviation for the state of Alabama?

State abbreviations are all two letters, and these two letters are always capitalized without any periods. So, New Hampshire is shortened as NH and not as N.H. with periods after each letter. Similarly, the Alabama abbreviation is AL (and not Al). Here is a list of all 50 US state abbreviations plus the territories: Alabama - AL; Alaska - AK ...


1 Answers

I worked around it by finding the last 2 letters word in the full address (assuming an US address provided by google maps android geocoder). It works for all cases I have found:

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;
}
like image 166
pellyadolfo Avatar answered Oct 10 '22 01:10

pellyadolfo