Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 582
Frederik Jørgensen Avatar asked Mar 01 '15 13:03

Frederik Jørgensen


2 Answers

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)
  }
}
like image 53
Letaief Achraf Avatar answered Sep 28 '22 18:09

Letaief Achraf


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.

  • SPGooglePlacesAutocomplete (https://github.com/spoletto/SPGooglePlacesAutocomplete) is very popular and has many features, but it's written in Objective-C
  • iOS Google Places Autocomplete (https://github.com/watsonbox/ios_google_places_autocomplete) is more recent, written in Swift, and also contains some examples.
  • lots of others...

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

like image 39
Romain Avatar answered Sep 28 '22 19:09

Romain