在我的Swift iOS应用程序中,我想让用户放置自动完成搜索屏幕,让他们在上下文中搜索他们当前的位置。显然,Google Place Autocomplete无法实现这一点,因为无法将当前位置上下文传递给它。
我的第二选择是使用Google地方选择器的搜索屏幕,因为当我开始以当前位置为中心的地方选择器然后点按搜索时,它会搜索当前位置的上下文位置。
我的问题是,是否可以将用户直接带到地方选择器的搜索屏幕,然后在抓取所选地点信息后关闭地方选择器,避开地方选择器的主UI?
答案 0 :(得分:0)
在文档中有点令人困惑,但我认为你想要的是使用GMSAutocompleteViewController而不是地方选择器。
以下示例代码,指向文档here的链接。
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
}
}