簡體   English   中英

Swift Google Place Autocomplete在Google Maps中不起作用

[英]Swift Google Place Autocomplete not working wtih Google Maps

我正在使用搜索欄來使用Google Place自動完成功能。 但是,當我將搜索欄放在GMSMapView頂部時,它不起作用。 當我注釋掉loadView()函數時,它完全可以正常工作。 有沒有辦法在Google地圖上使用地點自動完成功能?

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

上面的代碼來自https://developers.google.com/maps/documentation/ios-sdk/maphttps://developers.google.com/places/ios-api/autocomplete 我復制了這些文件進行測試,但仍然無法正常工作。

抱歉,我還沒有發表評論的聲譽,但是我認為這可以幫助您解決問題。

首先調用super.loadView(),然后在初始化GMSMapView時必須設置幀大小。 最后,將mapView添加到子視圖中。

    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)
   }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM