我想将CLPlacemark
格式化为字符串。
众所周知的方法是使用ABCreateStringWithAddressDictionary
,但在iOS 9中已弃用。警告告诉我改为使用CNPostalAddressFormatter
。
但是,CNPostalAddressFormatter
只能格式化CNPostalAddress
。无法将CLPlacemark
正确转换为CNPostalAddress
; CLPlacemark
和CNPostalAddress
共享这3个属性:country
,ISOcountryCode
和postalCode
。
那么我应该如何将CLPlacemark
格式化为现在的字符串?
答案 0 :(得分:26)
取地标的addressDictionary
并使用其"FormattedAddressLines"
键提取地址字符串。请注意,这是字符串行的数组。
(但是,你是正确的,负责转换到Contacts框架的Apple开发人员似乎完全忘记了Address Book和CLPlacemark之间的交换。这是Contacts框架中的一个严重错误 - 许多人中的一个。)
编辑由于我最初发布了这个答案,Apple修复了这个错误。 CLPlacemark现在具有postalAddress
属性,这是一个CNPostalAddress,然后您可以使用CNPostalAddressFormatter来获取一个漂亮的多行地址字符串。一定要import Contacts
!
答案 1 :(得分:14)
if let lines = myCLPlacemark.addressDictionary?["FormattedAddressLines"] as? [String] {
let placeString = lines.joined(separator: ", ")
// Do your thing
}
答案 2 :(得分:7)
extension String {
init?(placemark: CLPlacemark?) {
// Yadda, yadda, yadda
}
}
我第一次去移植AddressDictionary方法,就像其他海报一样。但这意味着失去CNPostalAddress
类和格式化程序的功能和灵活性。因此,方法2。
extension String {
// original method (edited)
init?(depreciated placemark1: CLPlacemark?) {
// UPDATE: **addressDictionary depreciated in iOS 11**
guard
let myAddressDictionary = placemark1?.addressDictionary,
let myAddressLines = myAddressDictionary["FormattedAddressLines"] as? [String]
else { return nil }
self.init(myAddressLines.joined(separator: " "))
}
// my preferred method - let CNPostalAddressFormatter do the heavy lifting
init?(betterMethod placemark2: CLPlacemark?) {
// where the magic is:
guard let postalAddress = CNMutablePostalAddress(placemark: placemark2) else { return nil }
self.init(CNPostalAddressFormatter().string(from: postalAddress))
}
}
等等,CLPlacemark
→CNPostalAddress
初始化程序是什么?
extension CNMutablePostalAddress {
convenience init(placemark: CLPlacemark) {
self.init()
street = [placemark.subThoroughfare, placemark.thoroughfare]
.compactMap { $0 } // remove nils, so that...
.joined(separator: " ") // ...only if both != nil, add a space.
/*
// Equivalent street assignment, w/o flatMap + joined:
if let subThoroughfare = placemark.subThoroughfare,
let thoroughfare = placemark.thoroughfare {
street = "\(subThoroughfare) \(thoroughfare)"
} else {
street = (placemark.subThoroughfare ?? "") + (placemark.thoroughfare ?? "")
}
*/
city = placemark.locality ?? ""
state = placemark.administrativeArea ?? ""
postalCode = placemark.postalCode ?? ""
country = placemark.country ?? ""
isoCountryCode = placemark.isoCountryCode ?? ""
if #available(iOS 10.3, *) {
subLocality = placemark.subLocality ?? ""
subAdministrativeArea = placemark.subAdministrativeArea ?? ""
}
}
}
func quickAndDirtyDemo() {
let location = CLLocation(latitude: 38.8977, longitude: -77.0365)
CLGeocoder().reverseGeocodeLocation(location) { (placemarks, _) in
if let address = String(depreciated: placemarks?.first) {
print("\nAddress Dictionary method:\n\(address)") }
if let address = String(betterMethod: placemarks?.first) {
print("\nEnumerated init method:\n\(address)") }
}
}
/* Output:
Address Dictionary method:
The White House 1600 Pennsylvania Ave NW Washington, DC 20500 United States
Enumerated init method:
1600 Pennsylvania Ave NW
Washington DC 20500
United States
*/
直到这里读到的人都会得到一件免费的T恤。 (不是真的)
*此代码适用于Swift 3& 4,除了用于删除nil值的flatMap
已在Swift 4.1中被折旧/重命名为compactMap
(文档here,或者请参阅SE-187了解原因)。
答案 3 :(得分:2)
Swift 3.0助手方法
class func addressFromPlacemark(_ placemark:CLPlacemark)->String{
var address = ""
if let name = placemark.addressDictionary?["Name"] as? String {
address = constructAddressString(address, newString: name)
}
if let city = placemark.addressDictionary?["City"] as? String {
address = constructAddressString(address, newString: city)
}
if let state = placemark.addressDictionary?["State"] as? String {
address = constructAddressString(address, newString: state)
}
if let country = placemark.country{
address = constructAddressString(address, newString: country)
}
return address
}