Get searched places by GoogleApi to fill in tableview

356 views Asked by At

I'v tried GMSPlacesClient but I couldn't get results in correct format to fill'em in tableView.

after that I used Fetcher but i didnt help me.

extension SearchViewController: GMSAutocompleteFetcherDelegate {
func didAutocomplete(with predictions: [GMSAutocompletePrediction]) {
    let resultsStr = NSMutableString()
    for prediction in predictions {
        resultsStr.appendFormat("%@\n", prediction.attributedPrimaryText)
    }

     print(resultsStr as String)
}

func didFailAutocompleteWithError(_ error: Error) {
    print(error.localizedDescription)
}

}

i don't know if i didn't understand it's usage. someone maybe describes me how to use Google Places Api to search Address in textBox and then show results in tableView.

My problem is onely getting results of search?


fetch Part Code:

var fetcher: GMSAutocompleteFetcher?

func placeAutocomplete(add:String) {
    fetcher?.sourceTextHasChanged(add)
}

override func viewDidLoad() {
    super.viewDidLoad()
    tblView.delegate = self
    tblView.dataSource = self


    let filter = GMSAutocompleteFilter()
    filter.type = .establishment

    // Create the fetcher.
    fetcher = GMSAutocompleteFetcher(bounds: nil, filter: filter)
    fetcher?.delegate = self
}

result for search "Mex"

> Me{
GMSAutocompleteMatch = "<GMSAutocompleteMatchFragment: 0x60800043db60>";
}rcado de La Boqueria, La Rambla, Barcelona, Spain{
}*
Me{
  GMSAutocompleteMatch = "<GMSAutocompleteMatchFragment: 
   0x60800043db60>";
}rcado San Anton, Calle de Augusto Figueroa, Madrid, Spain{
}*
     Me{
 GMSAutocompleteMatch = "<GMSAutocompleteMatchFragment: 0x60800043db60>";
 }nlyn Mall, Atterbury Road, Pretoria, South Africa{
}*
    Me{
   GMSAutocompleteMatch = "<GMSAutocompleteMatchFragment: 
  0x60800043db60>";
}ga Mall, Bulevardul Pierre de Coubertin, Bucharest, Romania{
    }*
      Me{
  GMSAutocompleteMatch = "<GMSAutocompleteMatchFragment: 
    0x60800043db60>";
    }rcado de San Ildefonso, Calle de Fuencarral, Madrid, Spain{
  }* 

print code :

for q in predictions1
{
   print ( "\(q.attributedFullText)*")
}
2

There are 2 answers

4
Sandy On

you should use array for results instead of let resultsStr = NSMutableString()

and populate table view with data source as array

0
Aamir On

You need to tweak with GMSAutocompleteFilter filter to get desired results. Currently you are filtering results with filter type: .establishment, which filters/limits search results. You may consider using .noFilter or any other filter.

let filter = GMSAutocompleteFilter()
filter.type = .noFilter

Reference to enum GMSPlacesAutocompleteTypeFilter, Source: GMSAutocompleteFilter.h

public enum GMSPlacesAutocompleteTypeFilter : Int {


    /**
     * All results.
     */
    case noFilter

    /**
     * Geeocoding results, as opposed to business results.
     */
    case geocode

    /**
     * Geocoding results with a precise address.
     */
    case address

    /**
     * Business results.
     */
    case establishment

    /**
     * Results that match the following types:
     * "locality",
     * "sublocality"
     * "postal_code",
     * "country",
     * "administrative_area_level_1",
     * "administrative_area_level_2"
     */
    case region

    /**
     * Results that match the following types:
     * "locality",
     * "administrative_area_level_3"
     */
    case city
}