Saurav Dutta
Saurav Dutta

Reputation: 3

Swift google autocomplete with local search

I am trying to do Google Autocomplete using Google Places in Swift 3.0. But I need to search depending upon my current location. Example, If I am in Kolkata, India and I type search keyword "Ko" it will show the results of Kolkata first .

Can anyone help me. Here is my code.I import GooglePlaces in my class

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

Please anyone help me to solve it out. Thanks in advance.

Upvotes: 0

Views: 1386

Answers (1)

nathan
nathan

Reputation: 9385

The only API provided by GMSAutocompleteViewController is to set the GMSCoordinateBounds like so (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)
}

Upvotes: 1

Related Questions