How to add google places autocomplete to xcode with swift (tutorial)

I want to add google places autocomplete to xcode with swift, so users can search on a city and press enter, so should the app show that city on the map.

I´m using google maps, so it has to be connected to that map and the search bar would i like to have in the navi-bar (just above the map)

Does anyone know a good tutorial to do that??

Upvotes: 13

Views: 35740

Answers (8)

Kuldeep Tanwar
Kuldeep Tanwar

Reputation: 3526

Simple and lightweight solution for Swift users !

I also created a library to make these simple request without including any framework of third party library. You can also maintain cache for Autocomplete with the library.

To use to library follow these steps : -

step-1 Import GoogleApiHelper into your project.

step-2 Initialise GoogleApiHelper

GoogleApi.shared.initialiseWithKey("API_KEY")

step-3 Call the methods

var input = GInput()
input.keyword = "San francisco"
GoogleApi.shared.callApi(input: input) { (response) in
    if let results = response.data as? [GApiResponse.Autocomplete], response.isValidFor(.autocomplete) {
        //Enjoy the Autocomplete Api
    } else { print(response.error ?? "ERROR") }
}

You can find the library here

enter image description here

Upvotes: 0

Letaief Achraf
Letaief Achraf

Reputation: 650

For Swift 3:

1- Select your podfile and type : pod 'GooglePlaces'

2- In the appDelegate, add your API Key : GMSPlacesClient.provideAPIKey("YOUR KEY") (Import the GooglePlaces)

3- Use this code in your viewController that contains the googleMap:

    // This code snippet demonstrates adding a
// full-screen Autocomplete UI control

import UIKit
import GooglePlaces

class ViewController: UIViewController {

  // TODO: Add a button to Main.storyboard to invoke onLaunchClicked.

  // Present the Autocomplete view controller when the button is pressed.
  @IBAction func onLaunchClicked(sender: UIButton) {
    let acController = GMSAutocompleteViewController()
    acController.delegate = self
    present(acController, animated: true, completion: nil)
  }
}

extension ViewController: GMSAutocompleteViewControllerDelegate {

  // Handle the user's selection.
  func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
    print("Place name: \(place.name)")
    print("Place address: \(place.formattedAddress)")
    print("Place attributions: \(place.attributions)")
    dismiss(animated: true, completion: nil)
  }

  func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) {
    // TODO: handle the error.
    print("Error: \(error)")
    dismiss(animated: true, completion: nil)
  }

  // User cancelled the operation.
  func wasCancelled(_ viewController: GMSAutocompleteViewController) {
    print("Autocomplete was cancelled.")
    dismiss(animated: true, completion: nil)
  }
}

Upvotes: 8

Ronak Kalavadia
Ronak Kalavadia

Reputation: 397

Use this latest repo. with swift 2.0

Just download zip file of project and open pod file and write pod 'GoogleMaps' and save it open terminal and install pod.

Enjoy!!!

RKAutoCompletePlaceSearch

Upvotes: 1

AndrewR
AndrewR

Reputation: 10889

Since the question was asked, Google has added UI elements to the iOS SDK for this sort of workflow. Check out the documentation.

Upvotes: 0

Mrug
Mrug

Reputation: 5023

You can try one for wrapper to implement Autocompletion for Place API with Google:

https://github.com/mrugrajsinh/MVAutocompletePlaceSearchTextField

Its very simple drop-in control and subclass of UITextField so using by binding Class with simple UITextField you can achive Auto completion dropdown similar as Uber and many popular app does.

Upvotes: 5

Howard Wilson
Howard Wilson

Reputation: 143

Here's a tutorial I came across which is based on my ios_google_places_autocomplete code.

@Frederik: If you've had a problem with the library, please create an issue describing it.

Upvotes: 3

Romain
Romain

Reputation: 3758

Not exactly tutorials, but there are a few resources on Github, with people providing libraries and code samples to achieve what you're asking for.

Even without proper tutorials, you can still check out these libraries and understand how their code works to get some inspiration if you want to write your own components.

As a sidetone, Google also has this page, but it's not a dedicated tutorial for iOS apps, just some useful explanations of how their API works: https://developers.google.com/places/documentation/autocomplete#examples

Upvotes: 7

Related Questions