Fate Riddle Fate Riddle - 4 months ago 23
iOS Question

How do you read FULL address using coreLocation ( iOS/ Swift )

Here's the code:

CLGeocoder().reverseGeocodeLocation(myCurrentLoc) { (array, err) in

print(err)
if err != nil {
print(err)
return
}

if let place = array?.first {

if let locality = place.locality {
if let country = place.country {
if let addr = place.addressDictionary {
print(addr)
}
let myAddress = "\(locality) \(country)"
self.adress.text = myAddress
self.locMgr.stopUpdatingLocation()

}
}

}
self.locMgr.stopUpdatingLocation()

}


So I can read longitude/latitude, and the only thing I need is the full address. Right now I can print out "New York, US", I find all the information I need is in place.addressDictionary, which is a dictionary. But I can't seem to read anything from that dictionary using normal syntax. How do I do it?

Answer

I usually make a location manager helper to fetch details and use a placemark

The dictionary you are talking about comes with placemarks

Here is my Model

class UserLocation: NSObject {

 var latitude : String?
 var longitude: String?
 var timeZone: String?
 var city : String?
 var state : String?
 var country : String?
 // you can and more fields of placemark

required init(latitude:String?,longitude:String?,timezone:String?,city:String?,state:String?,country:String?) {
    self.latitude = latitude
    self.longitude = longitude
    self.timeZone = timezone
    self.city = city
    self.state = state
    self.country = country
   }


}

And then I use the below code to reverse geocode after getting a location from Location Manager and use placemarks to get details

//Location Manager delegate
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    manager.stopUpdatingLocation()

    CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: {(placemarks, error)->Void in

        if (error != nil) {
            print("Reverse geocoder failed with error" + error!.localizedDescription)
            return
        }
        if placemarks!.count > 0 {
            let pm = placemarks![0]
            if let location = manager.location {
                let userLoc = UserLocation(latitude: String(location.coordinate.latitude), longitude: String(location.coordinate.longitude), timezone: NSTimeZone.localTimeZone().name, city: pm.locality!, state: pm.administrativeArea!, country:pm.country!)

                //By printing this dictionary you will get the complete address
                print(pm.addressDictionary)

                print(userLoc.city!)
                print(userLoc.state!)
                print(userLoc.country!)
                print(userLoc.latitude!)
                print(userLoc.longitude!)
                print(userLoc.timeZone!)

            } else {
                //Handle error
            }
            if(!CLGeocoder().geocoding){
                CLGeocoder().cancelGeocode()
            }
        }else{
            print("Problem with the data received from geocoder")
        }
}

Also you can fetch other properties of placemark like

enter image description here

enter image description here

I also printed the addressDictionary and you can see the complete address in the console in the last image