gpt4 book ai didi

ios - 如何获取当前 CLLocation 或 CLPlacemark 的 USA Region 和 Division?

转载 作者:行者123 更新时间:2023-11-28 07:52:53 24 4
gpt4 key购买 nike

我有一个名为 Address 的结构,我会在其中传递一个 CLPlacemark 作为参数。这里的问题是我无法从现有的 iOS 框架和类中获取 Region 或 Division。

struct Address: Codable {

var street: String?
var city: String?
var state: String?
var zip_code: String?
var formatted_address: String?
var latitude: Double?
var longitude: Double?
var region: String?
var division: String?

init?(placemark: CLPlacemark) {
var address = ""

if let address1 = placemark.thoroughfare {
self.street = address1
address += " \(address1),"
}

if let address2 = placemark.subThoroughfare {
address += " \(address2),"
self.street = address2
}

if let city = placemark.locality {
address += " \(city),"
self.city = city
}

if let state = placemark.administrativeArea {
address += " \(state),"
self.state = state
}

if let zip = placemark.postalCode {
address += " \(zip)"
self.zip_code = zip
}

if let location = placemark.location {
let coordinates = location.coordinate
self.latitude = coordinates.latitude
self.longitude = coordinates.longitude
}

self.formatted_address = address

}

var coordinates: CLLocationCoordinate2D? {
if let latitude = self.latitude, let longitude = self.longitude {
return CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
}

return nil
}
}

我怎样才能做到这一点?我在想一种方法是使用字典列出区域和分区,其中包含状态数组,如果包含数组的字典包含状态,那么它就是区域/分区。有没有更好的解决方案?

最佳答案

为此我得到的唯一解决方案是声明两个 String: [String] 字典的静态数组。一个是区域数组,另一个是分区数组。

它看起来像下面这样:

let regions: [String: [String]] = [
"Northeast": [
"New England", "Middle Atlantic"
],
"Midwest": [
"East North Central", "West North Central"
],
"South": [
"South Atlantic", "East South Central", "West South Central"
],
"West": [
"Mountain", "Pacific"
],
]

let divisions: [String: [String]] = [
"New England": [
"CT", "MA", "ME", "NH", "RI", "VT"
],
"Middle Atlantic": [
"NJ", "NY", "PA"
],
"East North Central": [
"IL", "IN", "MI", "OH", "WI"
],
"West North Central": [
"IA", "KS", "MN", "MO", "ND", "NE", "SD"
],
"South Atlantic": [
"DC", "DE", "FL", "GA", "MD", "NC", "SC", "VA", "WV"
],
"East South Central": [
"AL", "KY", "MS", "TN"
],
"West South Central": [
"AR", "LA", "OK", "TX"
],
"Mountain": [
"AZ", "CO", "ID", "MT", "NM", "NV", "UT", "WA"
],
"Pacific": [
"AK", "CA", "HI", "OR", "WA"
]
]

然后从分区和区域中获取正确的值。我会使用从地标获得的状态。

if let state = placemark.administrativeArea {
address += " \(state),"
self.state = state
self.division = self.divisions.filter({ $0.value.contains(state) }).first?.key
if let division = self.division {
self.region = self.regions.filter({ $0.value.contains(division) }).first?.key
}
}

目前我找不到更好的解决方案,但如果你们有任何建议,我很乐意知道。

关于ios - 如何获取当前 CLLocation 或 CLPlacemark 的 USA Region 和 Division?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49230518/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com