CLGeocoder: US state Names are coming as a short codes
Asked Answered
H

3

5

I am developing app in iOS 8.0.

By below link, I am assigning the location object, How can I get current location from user in iOS

My aim is to find out City, State and Country name, By current location. Using answer of this link, I am getting the place mark objects used in Apple's Geocoder. Get current city and country from CLGeocoder?

I am getting everything alright, just

When I am getting the state name for Country United States. It gives me shortcodes, rather than giving me full state names.

e.g. For Alaska it gives me AK For Alabama it gives me AL

Like this is my log,

 North Kingstown, RI, United States

I want full state names, because I want to use full state name in my project.

I am getting state name by this code,

 NSString *stateName=myPlacemark.administrativeArea;
Hew answered 1/7, 2015 at 10:40 Comment(2)
Now I am using Google API for getting the city name, below is a link.Ultramicroscopic
#31161003Ultramicroscopic
P
5

Swift 5 enum with USA states list + ISO codes:

https://gist.github.com/SergLam/a252dd01324b5e2c04ecbe79cbb6b8fb

// https://en.wikipedia.org/wiki/List_of_U.S._state_and_territory_abbreviations
// https://en.wikipedia.org/wiki/ISO_3166-2

// NOTE: - Last two symbols from ISO 3166-2 state code
enum USAState: Int, CaseIterable {
    
    case alabama = 1
    case alaska = 2
    case arizona = 3
    case arkansas = 4
    case california = 5
    case colorado = 6
    case connecticut = 7
    case delaware = 8
    case districtOfColumbia = 9
    case florida = 10
    case georgia = 11
    case hawaii = 12
    case idaho = 13
    case illinois = 14
    case indiana = 15
    case iowa = 16
    case kansas = 17
    case kentucky = 18
    case louisiana = 19
    case maine = 20
    case maryland = 21
    case massachusetts = 22
    case michigan = 23
    case minnesota = 24
    case mississippi = 25
    case missouri = 26
    case montana = 27
    case nebraska = 28
    case nevada = 29
    case newHampshire = 30
    case newJersey = 31
    case newMexico = 32
    case newYork = 33
    case northCarolina = 34
    case northDakota = 35
    case ohio = 36
    case oklahoma = 37
    case oregon = 38
    case pennsylvania = 39
    case rhodeIsland = 40
    case southCarolina = 41
    case southDakota = 42
    case tennessee = 43
    case texas = 44
    case utah = 45
    case vermont = 46
    case virginia = 47
    case washington = 48
    case westVirginia = 49
    case wisconsin = 50
    case wyoming = 51
    
    var displayName: String {
        switch self {
        case .alabama:
            return "Alabama"
        case .alaska:
            return "Alaska"
        case .arizona:
            return "Arizona"
        case .arkansas:
            return "Arkansas"
        case .california:
            return "California"
        case .colorado:
            return "Colorado"
        case .connecticut:
            return "Connecticut"
        case .delaware:
            return "Delaware"
        case .districtOfColumbia:
            return "District of Columbia"
        case .florida:
            return "Florida"
        case .georgia:
            return "Georgia"
        case .hawaii:
            return "Hawaii"
        case .idaho:
            return "Idaho"
        case .illinois:
            return "Illinois"
        case .indiana:
            return "Indiana"
        case .iowa:
            return "Iowa"
        case .kansas:
            return "Kansas"
        case .kentucky:
            return "Kentucky"
        case .louisiana:
            return "Louisiana"
        case .maine:
            return "Maine"
        case .maryland:
            return "Maryland"
        case .massachusetts:
            return "Massachusetts"
        case .michigan:
            return "Michigan"
        case .minnesota:
            return "Minnesota"
        case .mississippi:
            return "Mississippi"
        case .missouri:
            return "Missouri"
        case .montana:
            return "Montana"
        case .nebraska:
            return "Nebraska"
        case .nevada:
            return "Nevada"
        case .newHampshire:
            return "New Hampshire"
        case .newJersey:
            return "New Jersey"
        case .newMexico:
            return "New Mexico"
        case .newYork:
            return "New York"
        case .northCarolina:
            return "North Carolina"
        case .northDakota:
            return "North Dakota"
        case .ohio:
            return "Ohio"
        case .oklahoma:
            return "Oklahoma"
        case .oregon:
            return "Oregon"
        case .pennsylvania:
            return "Pennsylvania"
        case .rhodeIsland:
            return "Rhode Island"
        case .southCarolina:
            return "South Carolina"
        case .southDakota:
            return "South Dakota"
        case .tennessee:
            return "Tennessee"
        case .texas:
            return "Texas"
        case .utah:
            return "Utah"
        case .vermont:
            return "Vermont"
        case .virginia:
            return "Virginia"
        case .washington:
            return "Washington"
        case .westVirginia:
            return "West Virginia"
        case .wisconsin:
            return "Wisconsin"
        case .wyoming:
            return "Wyoming"
        }
    }
    
    var stateCode: String {
        switch self {
        case .alabama:
            return "AL"
        case .alaska:
            return "AK"
        case .arizona:
            return "AZ"
        case .arkansas:
            return "AR"
        case .california:
            return "CA"
        case .colorado:
            return "CO"
        case .connecticut:
            return "CT"
        case .delaware:
            return "DE"
        case .districtOfColumbia:
            return "DC"
        case .florida:
            return "FL"
        case .georgia:
            return "GA"
        case .hawaii:
            return "HI"
        case .idaho:
            return "ID"
        case .illinois:
            return "IL"
        case .indiana:
            return "IN"
        case .iowa:
            return "IA"
        case .kansas:
            return "KS"
        case .kentucky:
            return "KY"
        case .louisiana:
            return "LA"
        case .maine:
            return "ME"
        case .maryland:
            return "MD"
        case .massachusetts:
            return "MA"
        case .michigan:
            return "MI"
        case .minnesota:
            return "MN"
        case .mississippi:
            return "MS"
        case .missouri:
            return "MO"
        case .montana:
            return "MT"
        case .nebraska:
            return "NE"
        case .nevada:
            return "NV"
        case .newHampshire:
            return "NH"
        case .newJersey:
            return "NJ"
        case .newMexico:
            return "NM"
        case .newYork:
            return "NY"
        case .northCarolina:
            return "NC"
        case .northDakota:
            return "ND"
        case .ohio:
            return "OH"
        case .oklahoma:
            return "OK"
        case .oregon:
            return "OR"
        case .pennsylvania:
            return "PA"
        case .rhodeIsland:
            return "RI"
        case .southCarolina:
            return "SC"
        case .southDakota:
            return "SD"
        case .tennessee:
            return "TN"
        case .texas:
            return "TX"
        case .utah:
            return "UT"
        case .vermont:
            return "VT"
        case .virginia:
            return "VA"
        case .washington:
            return "WA"
        case .westVirginia:
            return "WV"
        case .wisconsin:
            return "WI"
        case .wyoming:
            return "WY"
        }
    }
    
    var isoStateCode: String {
        switch self {
        default:
            return "USA-\(self.stateCode)"
        }
    }
    
}
Palais answered 2/11, 2020 at 12:26 Comment(0)
M
6

There is Swift 3 version of @Arpit B Parekh answer:

let stateCodes = ["AL","AK","AZ","AR","CA","CO","CT","DE","DC","FL","GA","HI","ID","IL","IN","IA","KS","KY","LA","ME","MD","MA","MI","MN","MS","MO","MT","NE","NV","NH","NJ","NM","NY","NC","ND","OH","OK","OR","PA","RI","SC","SD","TN","TX","UT","VT","VA","WA","WV","WI","WY"]
let fullStateNames = ["Alabama","Alaska","Arizona","Arkansas","California","Colorado","Connecticut","Delaware","District of Columbia","Florida","Georgia","Hawaii","Idaho","Illinois","Indiana","Iowa","Kansas","Kentucky","Louisiana","Maine","Maryland","Massachusetts","Michigan","Minnesota","Mississippi","Missouri","Montana","Nebraska","Nevada","New Hampshire","New Jersey","New Mexico","New York","North Carolina","North Dakota","Ohio","Oklahoma","Oregon","Pennsylvania","Rhode Island","South Carolina","South Dakota","Tennessee","Texas","Utah","Vermont","Virginia","Washington","West Virginia","Wisconsin","Wyoming"]

func shortStateName(_ state:String) -> String {
    let lowercaseNames = fullStateNames.map { $0.lowercased() }
    let dic = NSDictionary(objects: stateCodes, forKeys: lowercaseNames as [NSCopying])
    return dic.object(forKey:state.lowercased()) as? String ?? state}

func longStateName(_ stateCode:String) -> String {
    let dic = NSDictionary(objects: fullStateNames, forKeys:stateCodes as [NSCopying])
    return dic.object(forKey:stateCode) as? String ?? stateCode
}
Mickeymicki answered 20/5, 2017 at 14:21 Comment(0)
P
5

Swift 5 enum with USA states list + ISO codes:

https://gist.github.com/SergLam/a252dd01324b5e2c04ecbe79cbb6b8fb

// https://en.wikipedia.org/wiki/List_of_U.S._state_and_territory_abbreviations
// https://en.wikipedia.org/wiki/ISO_3166-2

// NOTE: - Last two symbols from ISO 3166-2 state code
enum USAState: Int, CaseIterable {
    
    case alabama = 1
    case alaska = 2
    case arizona = 3
    case arkansas = 4
    case california = 5
    case colorado = 6
    case connecticut = 7
    case delaware = 8
    case districtOfColumbia = 9
    case florida = 10
    case georgia = 11
    case hawaii = 12
    case idaho = 13
    case illinois = 14
    case indiana = 15
    case iowa = 16
    case kansas = 17
    case kentucky = 18
    case louisiana = 19
    case maine = 20
    case maryland = 21
    case massachusetts = 22
    case michigan = 23
    case minnesota = 24
    case mississippi = 25
    case missouri = 26
    case montana = 27
    case nebraska = 28
    case nevada = 29
    case newHampshire = 30
    case newJersey = 31
    case newMexico = 32
    case newYork = 33
    case northCarolina = 34
    case northDakota = 35
    case ohio = 36
    case oklahoma = 37
    case oregon = 38
    case pennsylvania = 39
    case rhodeIsland = 40
    case southCarolina = 41
    case southDakota = 42
    case tennessee = 43
    case texas = 44
    case utah = 45
    case vermont = 46
    case virginia = 47
    case washington = 48
    case westVirginia = 49
    case wisconsin = 50
    case wyoming = 51
    
    var displayName: String {
        switch self {
        case .alabama:
            return "Alabama"
        case .alaska:
            return "Alaska"
        case .arizona:
            return "Arizona"
        case .arkansas:
            return "Arkansas"
        case .california:
            return "California"
        case .colorado:
            return "Colorado"
        case .connecticut:
            return "Connecticut"
        case .delaware:
            return "Delaware"
        case .districtOfColumbia:
            return "District of Columbia"
        case .florida:
            return "Florida"
        case .georgia:
            return "Georgia"
        case .hawaii:
            return "Hawaii"
        case .idaho:
            return "Idaho"
        case .illinois:
            return "Illinois"
        case .indiana:
            return "Indiana"
        case .iowa:
            return "Iowa"
        case .kansas:
            return "Kansas"
        case .kentucky:
            return "Kentucky"
        case .louisiana:
            return "Louisiana"
        case .maine:
            return "Maine"
        case .maryland:
            return "Maryland"
        case .massachusetts:
            return "Massachusetts"
        case .michigan:
            return "Michigan"
        case .minnesota:
            return "Minnesota"
        case .mississippi:
            return "Mississippi"
        case .missouri:
            return "Missouri"
        case .montana:
            return "Montana"
        case .nebraska:
            return "Nebraska"
        case .nevada:
            return "Nevada"
        case .newHampshire:
            return "New Hampshire"
        case .newJersey:
            return "New Jersey"
        case .newMexico:
            return "New Mexico"
        case .newYork:
            return "New York"
        case .northCarolina:
            return "North Carolina"
        case .northDakota:
            return "North Dakota"
        case .ohio:
            return "Ohio"
        case .oklahoma:
            return "Oklahoma"
        case .oregon:
            return "Oregon"
        case .pennsylvania:
            return "Pennsylvania"
        case .rhodeIsland:
            return "Rhode Island"
        case .southCarolina:
            return "South Carolina"
        case .southDakota:
            return "South Dakota"
        case .tennessee:
            return "Tennessee"
        case .texas:
            return "Texas"
        case .utah:
            return "Utah"
        case .vermont:
            return "Vermont"
        case .virginia:
            return "Virginia"
        case .washington:
            return "Washington"
        case .westVirginia:
            return "West Virginia"
        case .wisconsin:
            return "Wisconsin"
        case .wyoming:
            return "Wyoming"
        }
    }
    
    var stateCode: String {
        switch self {
        case .alabama:
            return "AL"
        case .alaska:
            return "AK"
        case .arizona:
            return "AZ"
        case .arkansas:
            return "AR"
        case .california:
            return "CA"
        case .colorado:
            return "CO"
        case .connecticut:
            return "CT"
        case .delaware:
            return "DE"
        case .districtOfColumbia:
            return "DC"
        case .florida:
            return "FL"
        case .georgia:
            return "GA"
        case .hawaii:
            return "HI"
        case .idaho:
            return "ID"
        case .illinois:
            return "IL"
        case .indiana:
            return "IN"
        case .iowa:
            return "IA"
        case .kansas:
            return "KS"
        case .kentucky:
            return "KY"
        case .louisiana:
            return "LA"
        case .maine:
            return "ME"
        case .maryland:
            return "MD"
        case .massachusetts:
            return "MA"
        case .michigan:
            return "MI"
        case .minnesota:
            return "MN"
        case .mississippi:
            return "MS"
        case .missouri:
            return "MO"
        case .montana:
            return "MT"
        case .nebraska:
            return "NE"
        case .nevada:
            return "NV"
        case .newHampshire:
            return "NH"
        case .newJersey:
            return "NJ"
        case .newMexico:
            return "NM"
        case .newYork:
            return "NY"
        case .northCarolina:
            return "NC"
        case .northDakota:
            return "ND"
        case .ohio:
            return "OH"
        case .oklahoma:
            return "OK"
        case .oregon:
            return "OR"
        case .pennsylvania:
            return "PA"
        case .rhodeIsland:
            return "RI"
        case .southCarolina:
            return "SC"
        case .southDakota:
            return "SD"
        case .tennessee:
            return "TN"
        case .texas:
            return "TX"
        case .utah:
            return "UT"
        case .vermont:
            return "VT"
        case .virginia:
            return "VA"
        case .washington:
            return "WA"
        case .westVirginia:
            return "WV"
        case .wisconsin:
            return "WI"
        case .wyoming:
            return "WY"
        }
    }
    
    var isoStateCode: String {
        switch self {
        default:
            return "USA-\(self.stateCode)"
        }
    }
    
}
Palais answered 2/11, 2020 at 12:26 Comment(0)
H
1

As, I am only using Apple's GeoCoder. It is giving me only short codes of States of USA. e.g. CA for California.

I have used google but it is quite difficult to parse. The solution came from Wikipedia.https://en.wikipedia.org/wiki/List_of_states_and_territories_of_the_United_States

I have prepared below source to use the Full Name's from short codes.

NSArray *arrayShortCodes=[[NSArray alloc] initWithObjects:@"AL",@"AK",@"AZ",@"AR",@"CA",@"CO",@"CT",@"DE",@"DC",@"FL",@"GA",@"HI",@"ID",@"IL",@"IN",@"IA",@"KS",@"KY",@"LA",@"ME",@"MD",@"MA",@"MI",@"MN",@"MS",@"MO",@"MT",@"NE",@"NV",@"NH",@"NJ",@"NM",@"NY",@"NC",@"ND",@"OH",@"OK",@"OR",@"PA",@"RI",@"SC",@"SD",@"TN",@"TX",@"UT",@"VT",@"VA",@"WA",@"WV",@"WI",@"WY",nil];
    NSArray *arrayFullNames=[[NSArray alloc] initWithObjects:@"Alabama",@"Alaska",@"Arizona",@"Arkansas",@"California",@"Colorado",@"Connecticut",@"Delaware",@"District of Columbia",@"Florida",@"Georgia",@"Hawaii",@"Idaho",@"Illinois",@"Indiana",@"Iowa",@"Kansas",@"Kentucky",@"Louisiana",@"Maine",@"Maryland",@"Massachusetts",@"Michigan",@"Minnesota",@"Mississippi",@"Missouri",@"Montana",@"Nebraska",@"Nevada",@"New Hampshire",@"New Jersey",@"New Mexico",@"New York",@"North Carolina",@"North Dakota",@"Ohio",@"Oklahoma",@"Oregon",@"Pennsylvania",@"Rhode Island",@"South Carolina",@"South Dakota",@"Tennessee",@"Texas",@"Utah",@"Vermont",@"Virginia",@"Washington",@"West Virginia",@"Wisconsin",@"Wyoming", nil];

     dictionaryStateNames =[[NSDictionary alloc] initWithObjects:arrayFullNames forKeys:arrayShortCodes];

Now, we can easily access the fullname from short codes, using the valueForKey method of NSDictionary.

Hew answered 17/8, 2015 at 10:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.