使用本地搜索功能快速进行Google自动填充

时间:2017-09-04 16:58:22

标签: ios swift3 autocomplete google-places

我正在尝试使用Swift 3.0中的Google商家信息进行Google自动填充功能。但我需要根据我当前的位置进行搜索。例如,如果我在印度加尔各答,我输入搜索关键字" Ko"它将首先显示加尔各答的结果

任何人都可以帮助我。 这是我的代码。我在我的课程中导入GooglePlaces

@IBAction func txtFieldLocationDidStartEditing(_ sender: Any) {   
  self.placeAutocomplete()
}
func placeAutocomplete() {
    let autocompleteController = GMSAutocompleteViewController()
       autocompleteController.delegate = self
   present(autocompleteController, animated: true, completion: nil)
}

// MARK: - autoComplete Delegates

func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
    print("Place name: \(place.name)")
    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
}

请有人帮我解决。 提前谢谢。

1 个答案:

答案 0 :(得分:1)

GMSAutocompleteViewController提供的唯一API是将GMSCoordinateBounds设置为如此(reference):

func placeAutocomplete() {
    let visibleRegion = mapView.projection.visibleRegion()
    let bounds = GMSCoordinateBounds(coordinate: visibleRegion.farLeft, coordinate: visibleRegion.nearRight)

    let autocompleteController = GMSAutocompleteViewController()
    acController.autocompleteBounds = bounds
    autocompleteController.delegate = self
    present(autocompleteController, animated: true, completion: nil)
}