kishor0011 kishor0011 - 9 days ago 6
Swift Question

How to get city , state and Country name by Pincode in Swift iOS?

I have Five textFields and I want to autofill Area,City ,State and Country when I enter the pin code number.

import UIKit
import CoreLocation

class CreateGroupView: UIViewController,UITextFieldDelegate,CLLocationManagerDelegate
{
//View Did Load
override func viewDidLoad() {
super.viewDidLoad()

let location: String = "305621"//"yourPINCode"
let geocoder: CLGeocoder = CLGeocoder()
geocoder.geocodeAddressString(location,completionHandler: {(placemarks: [CLPlacemark]?, error: NSError?) -> Void in
if ((placemarks?.count)! > 0) {
let placemark: CLPlacemark = (placemarks?[0])!
let country : String = placemark.country!
let state: String = placemark.administrativeArea!
print(country)
print(state)
}
} as! CLGeocodeCompletionHandler)
}
}


How can I do that ?
Thanks

Answer

you can use CLGeocoder to get detail of address from PIN code, here is sample code.

   let location: String = "yourpincode"
let geocoder: CLGeocoder = CLGeocoder()
geocoder.geocodeAddressString(location, completionHandler: {(placemarks: [CLPlacemark]?, error: Error?) -> Void in
    if ((placemarks?.count)! > 0) {
        let placemark: CLPlacemark = (placemarks?[0])!
        let country : String = placemark.country!
        let state: String = placemark.administrativeArea!
        print(state);
    }
} as! CLGeocodeCompletionHandler)