import UIKit
import CoreLocation
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
func geocode(latitude: Double, longitude: Double, completion: @escaping (_ placemark: [CLPlacemark]?, _ error: Error?) -> Void) {
CLGeocoder().reverseGeocodeLocation(CLLocation(latitude: latitude, longitude: longitude)) { placemark, error in
guard let placemark = placemark, error == nil else {
completion(nil, error)
return
}
completion(placemark, nil)
}
}
或者简单地:
func geocode(latitude: Double, longitude: Double, completion: @escaping (_ placemark: [CLPlacemark]?, _ error: Error?) -> Void) {
CLGeocoder().reverseGeocodeLocation(CLLocation(latitude: latitude, longitude: longitude), completionHandler: completion)
}
或扩展CLLocation:
extension CLLocation {
func geocode(completion: @escaping (_ placemark: [CLPlacemark]?, _ error: Error?) -> Void) {
CLGeocoder().reverseGeocodeLocation(self, completionHandler: completion)
}
}
要将地标格式化为邮寄地址,可以使用“联系人”框架CNPostalAddressFormatter
:
import Contacts
extension Formatter {
static let mailingAddress: CNPostalAddressFormatter = {
let formatter = CNPostalAddressFormatter()
formatter.style = .mailingAddress
return formatter
}()
}
extension CLPlacemark {
var mailingAddress: String? {
return postalAddress?.mailingAddress
}
}
extension CNPostalAddress {
var mailingAddress: String {
return Formatter.mailingAddress.string(from: self)
}
}
包含CLPlacemark对象的数组。对于大多数地理编码请求,此数组应仅包含一个条目。但是,在无法将指定地址解析到单个位置的情况下,前向地理编码请求可能会返回多个地标对象。如果请求被取消或获取地标信息时出错,则此参数为nil。
有关CLPlacemark属性的更多信息,可以检查此CLPlacemark
用法:
let location = CLLocation(latitude: -22.963451, longitude: -43.198242)
location.geocode { placemark, error in
if let error = error as? CLError {
print("CLError:", error)
return
} else if let placemark = placemark?.first {
// you should always update your UI in the main thread
DispatchQueue.main.async {
// update UI here
print("name:", placemark.name ?? "unkNown")
print("address1:", placemark.thoroughfare ?? "unkNown")
print("address2:", placemark.subThoroughfare ?? "unkNown")
print("neighborhood:", placemark.subLocality ?? "unkNown")
print("city:", placemark.locality ?? "unkNown")
print("state:", placemark.administrativeArea ?? "unkNown")
print("subAdministrativeArea:", placemark.subAdministrativeArea ?? "unkNown")
print("zip code:", placemark.postalCode ?? "unkNown")
print("country:", placemark.country ?? "unkNown", terminator: "\n\n")
print("isoCountryCode:", placemark.isoCountryCode ?? "unkNown")
print("region identifier:", placemark.region?.identifier ?? "unkNown")
print("timezone:", placemark.timeZone ?? "unkNown", terminator:"\n\n")
// Mailind Address
print(placemark.mailingAddress ?? "unkNown")
}
}
}
这将打印
name: Morro da Saudade
address1: Rua Casuarina
address2: 597
neighborhood: Lagoa
city: Rio de Janeiro
state: RJ
subAdministrativeArea: unkNown
zip code: 22011-040
country: Brazil
isoCountryCode: BR
region identifier: <-22.96345100,-43.19824200> radius 141.83
timezone: America/Sao_Paulo (current)
Rua Casuarina,597
拉哥亚
里约热内卢
22011-040
巴西