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

匆匆过客 提交于 2019-11-27 21:53:35

问题


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?


回答1:


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.




回答2:


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



回答3:


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




回答4:


Looks like Google autocomplete Places API for iOS doesn't support filtering by country. GMSCoordinateBounds uses the northEast and southWest bounds corresponding to the rectangular region defined by the two corners.

It might be easier to use Google Places API Web Service and filter with components=country: You can specify what country you want after country:




回答5:


The full list of country codes can be found here:
https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2

Use it with:

let filter = GMSAutocompleteFilter()
filter.country = "YOURCountryCode"


来源:https://stackoverflow.com/questions/31678607/how-to-get-country-specific-result-with-google-autocomplete-place-api-ios-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!