S. Park
S. Park

Reputation: 149

Swift Google Place Autocomplete not working wtih Google Maps

I am using a search bar to use Google Place Autocomplete feature. However, it is not working when I am putting the search bar on top of GMSMapView. It works completely fine when I comment out loadView() function. Is there a way to use the place autocomplete with Google map?

import UIKit
import GoogleMaps

class ViewController: UIViewController {

  var resultsViewController: GMSAutocompleteResultsViewController?
  var searchController: UISearchController?
  var resultView: UITextView?

  override func loadView() {
    let camera = GMSCameraPosition.cameraWithLatitude(1.285, longitude: 103.848, zoom: 12)
    let mapView = GMSMapView.mapWithFrame(.zero, camera: camera)
    self.view = mapView
  }

  override func viewDidLoad() {
    super.viewDidLoad()

    resultsViewController = GMSAutocompleteResultsViewController()
    resultsViewController?.delegate = self

    searchController = UISearchController(searchResultsController: resultsViewController)
    searchController?.searchResultsUpdater = resultsViewController

    let subView = UIView(frame: CGRectMake(0, 65.0, 350.0, 45.0))

    subView.addSubview((searchController?.searchBar)!)
    self.view.addSubview(subView)
    searchController?.searchBar.sizeToFit()
    searchController?.hidesNavigationBarDuringPresentation = false

    // When UISearchController presents the results view, present it in
    // this view controller, not one further up the chain.
    self.definesPresentationContext = true
  }
}

// Handle the user's selection.
extension ViewController: GMSAutocompleteResultsViewControllerDelegate {
  func resultsController(resultsController: GMSAutocompleteResultsViewController,
    didAutocompleteWithPlace place: GMSPlace) {
      searchController?.active = false
      // Do something with the selected place.
      print("Place name: ", place.name)
      print("Place address: ", place.formattedAddress)
      print("Place attributions: ", place.attributions)
  }

  func resultsController(resultsController: GMSAutocompleteResultsViewController,
    didFailAutocompleteWithError error: NSError){
      // TODO: handle the error.
      print("Error: ", error.description)
  }

  // Turn the network activity indicator on and off again.
  func didRequestAutocompletePredictionsForResultsController(resultsController: GMSAutocompleteResultsViewController) {
    UIApplication.sharedApplication().networkActivityIndicatorVisible = true
  }

  func didUpdateAutocompletePredictionsForResultsController(resultsController: GMSAutocompleteResultsViewController) {
    UIApplication.sharedApplication().networkActivityIndicatorVisible = false
  }
}

The code above is from https://developers.google.com/maps/documentation/ios-sdk/map and https://developers.google.com/places/ios-api/autocomplete. I copied these for testing and is still not working.

Upvotes: 0

Views: 1703

Answers (1)

Juan Garcia
Juan Garcia

Reputation: 210

Sorry, I don't have the reputation to write comments yet, but I think this could help you to figure out the issue.

First call the super.loadView() and when you initialize the GMSMapView you must set a frame size. Finally add the mapView to the subview.

    override func loadView() {

    super.loadView()

    let camera = GMSCameraPosition.cameraWithLatitude(1.285, longitude: 103.848, zoom: 12)
    let mapView = GMSMapView.mapWithFrame(self.view.frame, camera: camera)
    self.view.addSubview(mapView)
   }

Upvotes: 1

Related Questions