1

I am using google place auto complete API to search addresses in my app. I wanted to restrict google place auto complete results to specific city (i.e searching addresses in new york only).

I know this can be done through the Bound coordinates but I don't know how to find it (i.e New york specific bound coordinates)

I searched through the documentation for how to restrict results to specific city(i.e New york only) but it's very confusing.

Note: I am using GMSAutoCompleteController for google auto complete API.

Please let me know how to accomplish it.

// Code :

 @IBAction func textfieldDidTapped(_ sender: Any) {
        listingAddressTextfield.resignFirstResponder()
        let controller = GMSAutocompleteViewController()
        let filter = GMSAutocompleteFilter()
        filter.type = .address
        controller.autocompleteFilter = filter
        controller.delegate = self
        present(controller, animated: true, completion: nil)
    }


// Delegate Methods...

extension ViewController: GMSAutocompleteViewControllerDelegate {
    func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
        listingAddressTextfield.text = place.name
        dismiss(animated: true, completion: nil)
    }
    func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) {
        print("Error: ", error.localizedDescription)
    }
    func wasCancelled(_ viewController: GMSAutocompleteViewController) {
        dismiss(animated: true, completion: nil)
    }
}
Jarvis The Avenger
  • 2,750
  • 1
  • 19
  • 37

1 Answers1

2

There are two properties of GMSAutocompleteViewController available to restrict and bias your location search.

User GMSCoordinateBounds which contains northEast and southWest coordinates. So you need northEast and southWest coordinate of New York city.

controller.autocompleteBounds = Instance of GMSCoordinateBounds

Then after you need to set

controller.autocompleteBoundsMode = .restrict

which will search places within your bounded area.

Sagar Chauhan
  • 5,715
  • 2
  • 22
  • 56