Reputation: 1612
I want to format CLPlacemark
to string.
The well known way is to use ABCreateStringWithAddressDictionary
but it was deprecated in iOS 9. Warning tells me to use CNPostalAddressFormatter
instead.
However, CNPostalAddressFormatter
can only format CNPostalAddress
. There is no way to properly convert CLPlacemark
to CNPostalAddress
; only these 3 properties are shared by CLPlacemark
and CNPostalAddress
: country
, ISOcountryCode
, and postalCode
.
So how should I format CLPlacemark
to string now?
Upvotes: 24
Views: 8589
Reputation: 534885
Take the placemark's addressDictionary
and use its "FormattedAddressLines"
key to extract the address string. Note that this is an array of the lines of the string.
(You are correct, however, that the Apple developers tasked with converting to the Contacts framework seem to have forgotten completely about the interchange between Address Book and CLPlacemark. This is a serious bug in the Contacts framework - one of many.)
EDIT Since I posted that answer originally, Apple fixed this bug. A CLPlacemark now has a postalAddress
property which is a CNPostalAddress, and you can then use a CNPostalAddressFormatter to get a nice multi-line address string. Be sure to import Contacts
!
Upvotes: 28
Reputation: 1698
extension String {
init?(placemark: CLPlacemark?) {
// Yadda, yadda, yadda
}
}
I first went for porting the AddressDictionary method, as did other posters. But that means losing the power and flexibility of the CNPostalAddress
class and formatter. Hence, method 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))
}
}
Wait, what is that CLPlacemark
→ CNPostalAddress
initializer??
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
*/
Whoever read until here gets a free T-shirt. (not really)
*This code works in Swift 3 & 4, except that flatMap
for removing nil values has been depreciated/renamed to compactMap
in Swift 4.1 (Doc here, or see SE-187 for the rationale).
Upvotes: 8
Reputation: 2896
if let lines = myCLPlacemark.addressDictionary?["FormattedAddressLines"] as? [String] {
let placeString = lines.joined(separator: ", ")
// Do your thing
}
Upvotes: 15
Reputation: 8322
Swift 3.0 Helper Method
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
}
Upvotes: 2