我有一个名为 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
}
}
我怎么能做到这一点?我在想一种方法是使用字典列出区域和分区,其中包含状态数组,如果保存数组的字典包含状态,那么它就是区域/分区。有没有更好的解决方案?