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?
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
!
if let lines = myCLPlacemark.addressDictionary?["FormattedAddressLines"] as? [String] { let placeString = lines.joined(separator: ", ") // Do your thing }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With