Reputation: 725
I am using this Tutorial by Google to implement Google Places Autocomplete into an iOS app with Swift but I am trying to restrict the results to a particular location as this link says is possible but for the life of me, I cannot see any place to do so..........
this is the view controller:
import UIKit
import GooglePlaces
class ViewController: UIViewController {
var resultsViewController: GMSAutocompleteResultsViewController?
var searchController: UISearchController?
var resultView: UITextView?
override func viewDidLoad() {
super.viewDidLoad()
resultsViewController = GMSAutocompleteResultsViewController()
resultsViewController?.delegate = self
searchController = UISearchController(searchResultsController: resultsViewController)
searchController?.searchResultsUpdater = resultsViewController
let subView = UIView(frame: CGRect(x: 0, y: 65.0, width: 350.0, height: 45.0))
subView.addSubview((searchController?.searchBar)!)
view.addSubview(subView)
searchController?.searchBar.sizeToFit()
searchController?.hidesNavigationBarDuringPresentation = false
// When UISearchController presents the results view, present it in
// this view controller, not one further up the chain.
definesPresentationContext = true
}
}
// Handle the user's selection.
extension ViewController: GMSAutocompleteResultsViewControllerDelegate {
func resultsController(_ resultsController: GMSAutocompleteResultsViewController,
didAutocompleteWith place: GMSPlace) {
searchController?.isActive = false
// Do something with the selected place.
print("Place name: \(place.name)")
print("Place address: \(place.formattedAddress)")
print("Place attributions: \(place.attributions)")
}
func resultsController(_ resultsController: GMSAutocompleteResultsViewController,
didFailAutocompleteWithError error: Error){
// TODO: handle the error.
print("Error: ", error.localizedDescription)
}
// Turn the network activity indicator on and off again.
func didRequestAutocompletePredictions(forResultsController resultsController: GMSAutocompleteResultsViewController) {
UIApplication.shared.isNetworkActivityIndicatorVisible = true
}
func didUpdateAutocompletePredictions(forResultsController resultsController: GMSAutocompleteResultsViewController) {
UIApplication.shared.isNetworkActivityIndicatorVisible = false
}
}
Upvotes: 0
Views: 2917
Reputation: 2376
Just to clarify on NebojsaNadj answer wrt to OP - You can first set the bounds for bias using points to represent the bounding box. So to bias the autocomplete to Omaha, Nebaska, for example:
let northEastBoundsCorner = //Omaha's NE corner in CLCoordinate2D
let southWestBoundsCorner = //Omaha's SW corner in CLCoordinate2D
let bounds = GMSCoordinateBounds(coordinate: neBoundsCorner,
coordinate: swBoundsCorner)
So when you instantiate the GMSController you just set it's auto complete bounds to the above boundsmyGMSAutoCompleteController.autocompleteBounds = bounds
. In the demo the OP referenced, it would be like this:
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()
//Here's where you add it
let northEastBoundsCorner = //Omaha's NE corner in CLCoordinate2D
let southWestBoundsCorner = //Omaha's SW corner in CLCoordinate2D
let bounds = GMSCoordinateBounds(coordinate: neBoundsCorner,
coordinate: swBoundsCorner)
controller.autocompleteBounds = bounds
//back to demo code
autocompleteController.delegate = self
present(autocompleteController, animated: true, completion: nil)
}
}
//see the demo for the rest of the code
Upvotes: 1
Reputation: 631
Right in the documentation it says what class is used for this.
A GMSCoordinateBounds object biasing the results to a specific area specified by latitude and longitude bounds.
NOTE: You cannot completely erase everything outside of these bounds, if people search for the eiffel tower, they will still see it. but 99% of the results will probably be within these bounds. It's just a bias search.
Give the GMSCoordinateBounds class corner longitude and latitudes, and that's it.
And it's used like this:
let northEastBoundsCorner = CLLocationCoordinate2D(latitude: -33.843366,
longitude: 151.134002)
let southWestBoundsCorner = CLLocationCoordinate2D(latitude: -33.875725,
longitude: 151.200349)
let bounds = GMSCoordinateBounds(coordinate: neBoundsCorner,
coordinate: swBoundsCorner)
func placeAutocomplete() {
let filter = GMSAutocompleteFilter()
filter.type = .establishment
placesClient.autocompleteQuery("Sydney Oper", bounds: bounds, filter: filter, callback: {(results, error) -> Void in
if let error = error {
print("Autocomplete error \(error)")
return
}
if let results = results {
for result in results {
print("Result \(result.attributedFullText) with placeID \(result.placeID)")
}
}
}) }
Here is the url from the documentation where they are using it: Set the GMSCoordinateBounds of autocomplete
Upvotes: 1