airsoftFreak airsoftFreak - 5 months ago 103
Swift Question

How to filter autocomplete country in Google maps ios sdk?

Currently , it queries in every country, but I want it to only query in Singapore

I found a stackoverflow solution How to get country specific result with Google autocomplete place api ios sdk?, but couldn't figure out where do i put the code.

Here's the code, I use the full screen solution for autocomplete.

import UIKit
import GoogleMaps

class OnlyLocationViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


@IBAction func clickMe(sender: UIButton) {
let autocompletecontroller = GMSAutocompleteViewController()
autocompletecontroller.delegate = self
self.presentViewController(autocompletecontroller, animated: true, completion: nil)


}

}

extension OnlyLocationViewController: GMSAutocompleteViewControllerDelegate{

func viewController(viewController: GMSAutocompleteViewController, didAutocompleteWithPlace place: GMSPlace) {
print("Place name: ", place.name)
print("Place address: ", place.formattedAddress)
print("Place attributions: ", place.attributions)

self.dismissViewControllerAnimated(true, completion: nil)
}


func viewController(viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: NSError) {
// To handle error
print(error)

}
func wasCancelled(viewController: GMSAutocompleteViewController) {
self.dismissViewControllerAnimated(true, completion: nil)
}

func didRequestAutocompletePredictions(viewController: GMSAutocompleteViewController) {
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
}

func didUpdateAutocompletePredictions(viewController: GMSAutocompleteViewController) {
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
}


}


Where should I put the filter code?

Answer

Try using this

@IBAction func clickMe(sender: UIButton) {
        let autocompletecontroller = GMSAutocompleteViewController()
        autocompletecontroller.delegate = self
        let filter = GMSAutocompleteFilter()
        filter.type = .Establishment  //suitable filter type
        filter.country = "SG"  //appropriate country code
        autocompletecontroller.autocompleteFilter = filter
        self.presentViewController(autocompletecontroller, animated: true, completion: nil)
    }

Hope it helps :)

Comments