1

在我的 Swift iOS 应用程序中,我想让用户放置自动完成搜索屏幕,让他们在上下文中搜索到他们当前的位置。显然,这对于 Google Place Autocomplete 是不可能的,因为无法将当前位置上下文传递给它。

我的第二个选择是使用 Google Place Picker 的搜索屏幕,因为当我以当前位置为中心启动 Place Picker 然后点击搜索时,它会在当前位置的上下文中搜索位置。

我的问题是,是否可以将用户直接带到 Place Picker 的搜索屏幕,然后在抓取到所选择的地点信息后关闭 Place Picker,避免 Place Picker 的主 UI?

4

1 回答 1

1

文档中有点令人困惑,但我认为您想要的是使用 GMSAutocompleteViewController 而不是地点选择器。

下面的示例代码,这里的文档链接。

import UIKit
import GooglePlaces

class ViewController: UIViewController {

  // Present the Autocomplete view controller when the button is pressed.
  @IBAction func autocompleteClicked(_ sender: UIButton) {
    let autocompleteController = GMSAutocompleteViewController()
    autocompleteController.delegate = self
    present(autocompleteController, 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.localizedDescription)
  }

  // User canceled the operation.
  func wasCancelled(_ viewController: GMSAutocompleteViewController) {
    dismiss(animated: true, completion: nil)
  }

  // Turn the network activity indicator on and off again.
  func didRequestAutocompletePredictions(_ viewController: GMSAutocompleteViewController) {
    UIApplication.shared.isNetworkActivityIndicatorVisible = true
  }

  func didUpdateAutocompletePredictions(_ viewController: GMSAutocompleteViewController) {
    UIApplication.shared.isNetworkActivityIndicatorVisible = false
  }

}
于 2017-02-10T14:27:53.140 回答