Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CLPlacemark to string in iOS 9

Tags:

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?

like image 859
Mostafa Mohamed Raafat Avatar asked Oct 27 '15 21:10

Mostafa Mohamed Raafat


2 Answers

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!

like image 119
matt Avatar answered Sep 21 '22 06:09

matt


Swift 3.0

if let lines = myCLPlacemark.addressDictionary?["FormattedAddressLines"] as? [String] {     let placeString = lines.joined(separator: ", ")     // Do your thing } 
like image 34
guido Avatar answered Sep 21 '22 06:09

guido