如何获取当前 CLLocation 或 CLPlacemark 的 USA Region 和 Division?
How to get USA Region and Division of current CLLocation or CLPlacemark?
我有一个名为 Address 的结构,我会在其中传递一个 CLPlacemark 作为参数。这里的问题是我无法从现有 iOS 框架和 类.
中获取区域或分区
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
}
}
我怎样才能做到这一点?我在想一种方法是使用字典列出区域和分区,其中包含状态数组,如果包含数组的字典包含状态,那么它就是 region/division。有没有更好的解决方案?
我得到的唯一解决方案是声明两个 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({ [=11=].value.contains(state) }).first?.key
if let division = self.division {
self.region = self.regions.filter({ [=11=].value.contains(division) }).first?.key
}
}
目前我找不到更好的解决方案,但如果你们有任何建议,我很乐意知道。
我有一个名为 Address 的结构,我会在其中传递一个 CLPlacemark 作为参数。这里的问题是我无法从现有 iOS 框架和 类.
中获取区域或分区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
}
}
我怎样才能做到这一点?我在想一种方法是使用字典列出区域和分区,其中包含状态数组,如果包含数组的字典包含状态,那么它就是 region/division。有没有更好的解决方案?
我得到的唯一解决方案是声明两个 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({ [=11=].value.contains(state) }).first?.key
if let division = self.division {
self.region = self.regions.filter({ [=11=].value.contains(division) }).first?.key
}
}
目前我找不到更好的解决方案,但如果你们有任何建议,我很乐意知道。