• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - CLGeocoder:美国各州名称以短代码形式出现

[复制链接]
菜鸟教程小白 发表于 2022-12-12 15:41:30 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在使用 iOS 8.0 开发应用程序。

通过下面的链接,我正在分配位置对象, How can I get current location from user in iOS

我的目标是通过当前位置找出城市、州和国家/地区的名称。 使用此链接的答案,我得到了 Apple 地理编码器中使用的地标对象。 Get current city and country from CLGeocoder?

我一切都好,只是

当我得到 Country United States 的州名时。 它给了我简码,而不是给我完整的州名。

例如对于 Alaska 它给了我 AK 对于 Alabama 它给了我 AL

就像这是我的日志,

 North Kingstown, RI, United States

我想要完整的州名,因为我想在我的项目中使用完整的州名。

我通过此代码获取州名,

 NSString *stateName=myPlacemark.administrativeArea;



Best Answer-推荐答案


带有美国州列表 + ISO 代码的 Swift 5 枚举:

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 "ennsylvania"
        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 "A"
        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)"
        }
    }
    
}

关于ios - CLGeocoder:美国各州名称以短代码形式出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31158998/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap