Reputation: 1773
I have the city name i.e. Austin, TX. I need to use that city as the bounds parameter when searching for establishments. Any idea how to do that?
let filter = GMSAutocompleteFilter()
filter.type = .establishment
placesClient.autocompleteQuery(textField.text!, bounds: nil, filter: filter, callback: {(results, error) -> Void in
if let error = error {
print("Autocomplete error \(error)")
return
}
if let results = results {
print(results)
}
})
Upvotes: 0
Views: 327
Reputation: 1371
I did not work with GMSAutocompleteFilter but I have implemented with google api, so here I am sharing some sample code, try this:
Take search bar & implement its delegate & call google places api as follows. Here, you need to pass the city name. So after calling this api you will get the results. With this results, I am storing place name in two different strings i.e. strMainTitle & strSubTitle. And finally reload the table to display list. Also you have to implement tableview delegate & datasource methods to display list on table.(Here you need to take tableview to display search text query).
// MARK:- SearchBar Delegate methods
func searchBar(searchBar: UISearchBar,textDidChange searchText: String)
{
let strCityString = "your city name"
let session = NSURLSession.sharedSession()
let apiServerKey = "Put here your apiServerkey"
var urlString = "https://maps.googleapis.com/maps/api/place/autocomplete/json?key=\(apiServerKey)&input=\(strCityString)"+"\(searchBarObj.text!)"
urlString = urlString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
session.dataTaskWithURL(NSURL(string: urlString)!) {
(data, response, error) -> Void in
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
do {
if let json = try NSJSONSerialization.JSONObjectWithData(data!, options:[]) as? [String:AnyObject] {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
let returnedPlaces: NSArray? = json["predictions"] as? NSArray// print("Predictions Retrunedplaces: \(returnedPlaces)")
self.arraySearchResults.removeAllObjects()
self.arraySearchResults.addObjectsFromArray(json["predictions"] as! [AnyObject])// (returnedPlaces!)
self.strMainTitle.removeAllObjects()
self.strSubTitle.removeAllObjects()
for index in 0..<returnedPlaces!.count {
if let returnedPlace = returnedPlaces?[index] as? NSDictionary {
var placeName = ""
if let name = returnedPlace["description"] as? NSString {
placeName = name as String
}
var fullNameArr = placeName.componentsSeparatedByString(",")
var strname = String()
let laststring:Int = fullNameArr.count - 1
for i in 1..<fullNameArr.count
{
if i == laststring //fullNameArr.count - 1
{
strname += "\(fullNameArr[i])"
}
else
{
strname += "\(fullNameArr[i])"+","
}
}
self.strMainTitle.addObject(placeName.componentsSeparatedByString(",").first!)
self.strSubTitle.addObject(strname)
}
}
self.tableviewForSearchBar.reloadData()
})
}
} catch {
}
}.resume()
}
Upvotes: 1