How To Implement Search Address with Google Autocomplete API in Swift?
You might be thinking that what’s the topic all about? Yes.
Let us tell you that this is one of the interesting
There are developers, who search a lot to find the best solution, but they find the solutions for the Objective-C.

Autocomplete is a
For example – if someone was typing the term Muhammad Ali (boxer) two days before his death, Google Autocomplete would've probably suggested something like Muhammad Ali (boxer) quote, while if he/she tried to search for his name after 30 minutes of his death, it would've supposedly suggested something like Muhammad Ali boxer death, because of sudden change in search patterns, which is related to the breaking news.
Most of the applications have google places search feature for that, we have to follow many
User
has to create server key and API key from Google Console through their or client’s account.Put below code in the controller in which you want to implement this feature.
Now, you need to take one text field to enter the specific keyword to get the results.
Lastly, follow the below instruction to achieve this feature.
a) Declare one operation queue in your swift file.
let operationQueue = NSOperationQueue()
b) Now you have to call below function when user type the keyword in textfield.
//MARK: Call this method on textfield did change
func beginSearching(searchText:String) {
if searchText.characters.count == 0 {
self.arrPlaces.removeAllObjects()
self.tblDisplayPlaces.reloadData()
return
}
operationQueue.addOperationWithBlock { () -> Void in
self.forwardGeoCoding(searchText)
}
}
c)Below function is called when you perform step(b). This function is passed the keyword wrote by user to google api and fetch related results in array.
//MARK: Search place from Google
func forwardGeoCoding(searchText:String) {
googlePlacesResult(searchText) { (result) -> Void in
let searchResult:NSDictionary = ["keyword":searchText,"results":result]
if result.count > 0
{
let features = searchResult.valueForKey("results") as! [AnyObject]
self.arrPlaces = NSMutableArray(capacity: 100)
for dictAddress in features {
self.arrPlaces.addObject(dictAddress.valueForKey("description")!)
}
dispatch_async(dispatch_get_main_queue(), {
self.tblDisplayPlaces.hidden = false;
self.tblDisplayPlaces.reloadData()
});
}
}
}
d)Below function is called when you perform step(c). This function perform to search the operation and fetch result using google autocomplete api.
//MARK: Google place API request
func googlePlacesResult(input: String, completion: (result: NSArray) -> Void) {
let googleAutoCompeteApi = https://maps.googleapis.com/maps/api/place/autocomplete/json?input=%@&types=address&language=en&key=%@"
let searchWordProtection = input.stringByReplacingOccurrencesOfString(" ", withString: "")
if searchWordProtection.characters.count != 0 {
let urlString = NSString(format: googleAutoCompeteApi, input,YOUR_SERVER_KEY)
let url = NSURL(string: urlString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!)
let defaultConfigObject = NSURLSessionConfiguration.defaultSessionConfiguration()
let delegateFreeSession = NSURLSession(configuration: defaultConfigObject, delegate: nil, delegateQueue: NSOperationQueue.mainQueue())
let request = NSURLRequest(URL: url!)
let task = delegateFreeSession.dataTaskWithRequest(request,
completionHandler: {
(data, response, error) -> Void in
if let data = data {
do {
let jSONresult = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments) as! [String:AnyObject]
let results:NSArray = jSONresult["predictions"] as! NSArray
let status = jSONresult["status"] as! String
if status == "NOT_FOUND" || status == "REQUEST_DENIED" {
let userInfo:NSDictionary = ["error": jSONresult["status"]!]
let newError = NSError(domain: "API Error", code: 666, userInfo: userInfo as [NSObject : AnyObject])
let arr:NSArray = [newError]
completion(result: arr)
return
} else {
completion(result: results)
}
}
catch {
print("json error: \(error)")
}
} else if let error = error {
print(error.description)
}
})
task.resume()
}
}

I wondered upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I’ll be subscribing to your feed and I hope you post again soon. ios App Development Company Android App Development Company
We Smarther offers excellent mobile app development services, the service that we offer will be form the hands of well experienced experts, we are delivering projects both on Android and ios based applications..