MyPage is a personalized page based on your interests.The page is customized to help you to find content that matters you the most.

I'm not curious

How To Implement Search Address with Google Autocomplete API in Swift?

Published on 16 June 16

You might be thinking that what’s the topic all about? Yes.

Let us tell you that this is one of the interesting topics we have for the developers. Here, we are going to explain what is Google Autocomplete and how to use in Swift to address something. Suppose, if a client wants to add address then she has to redirect to the Google Map and enter the keyword or character, so based on those keywords, google API respond to the corresponding places with the full address.

There are developers, who search a lot to find the best solution, but they find the solutions for the Objective-C. Swift programming language is trending, however, the developers have to follow long implementation process. Thus, we come up with a solution and implemented our own function that will help other developers as well.

Before That, Let’s Understand…
What is Google Autocomplete?
How To Implement Search Address with Google Autocomplete API in Swift? - Image 1

Autocomplete is a feature of the Places library in the Google Maps JavaScript API. Developers can use autocomplete to provide their applications the type-ahead-search behavior of the Google Maps search field. When any user starts typing an address, autocomplete will suggest in the rest.

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.

Here is the Solution of How to search places with Google Autocomplete API in Swift

Most of the applications have google places search feature for that, we have to follow many processes from google console account set up to develop a code. Here, we are developing a function for Swift. Users just have to require to put those function in their projects to get results from google autocomplete place API. Here is the step, how to implement this feature in easiest way:

  • 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 {





operationQueue.addOperationWithBlock { () -> Void in




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 {



dispatch_async(dispatch_get_main_queue(), {

self.tblDisplayPlaces.hidden = false;






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 ="

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)


} else {

completion(result: results)



catch {

print("json error: \(error)")


} else if let error = error {







This is a simple yet effective way to search address with Google Autocomplete API in Swift. However, if you find any difficult in Google Autocomplete process, let us know. We have experienced Swift app developers, who create and implement code to make the coding task easy. Recently, we have started iPhone app development in Swift. Till now, we have developed an app like TunerMoji and Smart Splash in Swift, as it requires less code.
We recommand you to have a look at this iOS 10 app development features for next version of iPhone app.
This blog is listed under Development & Implementations , Digital Media & Games and Mobility Community

View Comments (2)
Post a Comment

Please notify me the replies via email.

  • We hope the conversations that take place on will be constructive and thought-provoking.
  • To ensure the quality of the discussion, our moderators may review/edit the comments for clarity and relevance.
  • Comments that are promotional, mean-spirited, or off-topic may be deleted per the moderators' judgment.
  1. 07 September 17

    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

  2. 07 September 17

    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..

You may also be interested in
Awards & Accolades for MyTechLogy
Winner of
Top 100 Asia
Finalist at SiTF Awards 2014 under the category Best Social & Community Product
Finalist at HR Vendor of the Year 2015 Awards under the category Best Learning Management System
Finalist at HR Vendor of the Year 2015 Awards under the category Best Talent Management Software
Hidden Image Url