How to get an address from coordinates using MapKit?
I have this code when long press on the map it gets the coordinates:
func didLongPressMap(sender: UILongPressGestureRecognizer) {
if sender.state == UIGestureRecognizerState.Began {
let touchPoint = sender.locationInView(self.mapView)
let touchCoordinate = self.mapView.convertPoint(touchPoint, toCoordinateFromView: self.mapView)
var annotation = MKPointAnnotation()
annotation.coordinate = touchCoordinate
annotation.title = "Your position"
self.mapView.addAnnotation(annotation) //drops the pin
println("lat: \(touchCoordinate.latitude)")
var num = (touchCoordinate.latitude as NSNumber).floatValue
var formatter = NSNumberFormatter()
formatter.maximumFractionDigits = 4
formatter.minimumFractionDigits = 4
var str = formatter.stringFromNumber(num)
println("long: \(touchCoordinate.longitude)")
var num1 = (touchCoordinate.longitude as NSNumber).floatValue
var formatter1 = NSNumberFormatter()
formatter1.maximumFractionDigits = 4
formatter1.minimumFractionDigits = 4
var str1 = formatter1.stringFromNumber(num1)
self.adressLoLa.text = "\(num),\(num1)"
}
}
and I want to print in annotation.title
the complete address (street, city, zip, country).
Swift 4.2 Keep it as simple as possible, look at the Apple doc and modify it as you need:
Update Swift 4
addressDictionary was deprecated in iOS 11.0
More Data can be retrieved
name, thoroughfare, subThoroughfare, locality, subLocality, administrativeArea, subAdministrativeArea, postalcode, isoCountryCode, country, inlandWater, areaOfInterest
Thanks to @Kampi for this. This is an updated Swift 2.0 (Xcode 7) Version: