Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get country specific result with Google autocomplete place api ios sdk?

I'm using below method to get autocomplete result for input string of GMSPlacesClient class:

- (void)autocompleteQuery:(NSString *)query
               bounds:(GMSCoordinateBounds * GMS_NULLABLE_PTR)bounds
               filter:(GMSAutocompleteFilter * GMS_NULLABLE_PTR)filter
             callback:(GMSAutocompletePredictionsCallback)callback

I tried to implement bounds by multiple ways but none of them worked, I wonder if there is any standard way to get country specific results with GMSCoordinateBounds?

like image 869
Shrejas Avatar asked Jul 28 '15 14:07

Shrejas


4 Answers

You can get the country specific results with the Google autocomplete place api iOS SDK. Just go to this link

let filter = GMSAutocompleteFilter()
filter.type = .Establishment
filter.country = "UK"

set the filter country to what ever the country code you want. This will then fetch only country specific results for you.

You can obtain the country codes from this link

Hope this helps.

like image 106
Ankahathara Avatar answered Oct 21 '22 14:10

Ankahathara


In your google places API url, just append

 &components=country:sg 
here sg = country code for singapore. Append country code as per your requirement.

The country must be passed as a two character, ISO 3166-1 Alpha-2 compatible country code. you can refer Country code Of different countries

like image 29
Beena Avatar answered Oct 21 '22 14:10

Beena


This one work for me

let placesClient = GMSPlacesClient()
    let filter = GMSAutocompleteFilter()
    filter.type = .Establishment
    filter.country = "SG"
    placesClient.autocompleteQuery(searchString, bounds: nil, filter: filter) { (results, error:NSError?) -> Void in
      self.resultsArray.removeAll()
      if let _ = results {
        for result in results!{
          if let res: GMSAutocompletePrediction = result {
           // your code
          }
        }
      }
    }
like image 4
TMD Avatar answered Oct 21 '22 13:10

TMD


in Swift 5 and later

you can custom your search by configuring the GMSAutocompleteFilter, and specify the country from its attribute country

example :

let filter = GMSAutocompleteFilter()
filter.country = "MA"
autocompleteController.autocompleteFilter = filter

the country should be a ISO 3166-1 Alpha-2 country code (case insensitive).

you can find those codes from here: Countries ISO Codes

like image 1
SafoineMoncefAmine Avatar answered Oct 21 '22 13:10

SafoineMoncefAmine