1

我有一个用户填写的注册表单。我希望他们只需要输入他们的邮政编码并让我的程序能够推断他们的城市和州,这样他们就不必手动编写这些/拥有一个更简洁的界面和更少的输入字段。

我该怎么做?

4

3 回答 3

8

您可以为此使用 CoreLocation 框架。将邮政编码作为字符串,让框架做出最好的猜测,或者向用户展示猜测并让他们选择一个:

import UIKit
import CoreLocation

let zipCode = "1234AB"

let geocoder = CLGeocoder()
geocoder.geocodeAddressString(zipCode) {
    (placemarks, error) -> Void in
    // Placemarks is an optional array of CLPlacemarks, first item in array is best guess of Address

    if let placemark = placemarks?[0] {

        print(placemark.addressDictionary)
    }

}

请参阅CLGeoCoderCLPlaceMark

于 2016-04-10T07:37:05.403 回答
-3

您必须在您的应用程序中添加将城市与邮政编码相关联的参考(硬编码或在 plist 文件或核心数据中)。然后在用户输入邮政编码时检索并显示信息。

于 2016-04-10T07:29:10.947 回答
-3
**Not working. its not printing anything**

import UIKit
import CoreLocation

let zipCode = "1234AB"

let geocoder = CLGeocoder()
geocoder.geocodeAddressString(zipCode) {
    (placemarks, error) -> Void in
    // Placemarks is an optional array of CLPlacemarks, first item in array is best guess of Address

    if let placemark = placemarks?[0] {

        print(placemark.addressDictionary)
    }

}
于 2019-11-19T06:40:22.143 回答