1

我有一个名为 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
    }
}

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

4

1 回答 1

0

我得到的唯一解决方案就是声明两个静态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
            }
        }

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

于 2018-03-23T01:52:08.700 回答