获取GoogleApi搜索的地点以填写tableview

时间:2017-09-28 08:28:06

标签: ios swift google-places gmsplace

我尝试过GMSPlacesClient但是我无法以正确的格式获得结果以填充tableView。

之后我使用了Fetcher,但我没有帮助我。

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

}

我不知道我是否理解它的用法。有人可能会介绍我如何使用Google Places Api在textBox中搜索地址,然后在tableView中显示结果。

我的问题是获得搜索结果?

获取零件代码:

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
}

搜索结果“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{
  }* 

打印代码:

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

2 个答案:

答案 0 :(得分:1)

你应该使用数组来代替结果而不是让resultStr = NSMutableString()

并使用数据源填充表视图作为数组

答案 1 :(得分:0)

您需要使用GMSAutocompleteFilter过滤器调整以获得所需的结果。目前,您使用过滤器类型过滤结果:.establishment,过滤/限制搜索结果。您可以考虑使用.noFilter或任何其他过滤器。

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

参考enum GMSPlacesAutocompleteTypeFilter,来源: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
}