Переход от MKLocalSearch к MKLocalSearchCompleter - PullRequest
0 голосов
/ 15 ноября 2018

Я следовал руководству по настройке MKLocalSearchResponse, но вместо этого я хочу использовать MKLocalSearchCompleter в своем коде. Я нашел похожую проблему здесь , но я не могу перевести это в мой код.

Я хочу сделать это, потому что мне нужно больше результатов, когда клиент ищет адрес или аналогичный.

Может ли кто-нибудь дать мне некоторое понимание или подсказки к моей проблеме? Это может быть очень просто, но я пытался безуспешно.

class LocationSearchTable: UITableViewController {

weak var handleMapSearchDelegate: HandleMapSearch?
var matchingItems: [MKMapItem] = []
var mapView: MKMapView?
var localSearchResponse: MKLocalSearchResponse!

}

extension LocationSearchTable : UISearchResultsUpdating {

func updateSearchResults(for searchController: UISearchController) {
    guard let mapView = mapView,
        let searchBarText = searchController.searchBar.text else { return }



    let request = MKLocalSearchRequest()
    request.naturalLanguageQuery = searchBarText
    request.region = mapView.region
    let search = MKLocalSearch(request: request)

    search.start { localSearchResponse, _ in
        guard let response = localSearchResponse else {
            return
        }
        self.matchingItems = response.mapItems
        self.tableView.reloadData()
    }

}

}

extension LocationSearchTable {

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return matchingItems.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell")!
    let selectedItem = matchingItems[(indexPath as NSIndexPath).row].placemark
    cell.textLabel?.text = selectedItem.name
    cell.detailTextLabel?.text = parseAddress(selectedItem)
    return cell
}

}

extension LocationSearchTable {

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let selectedItem = matchingItems[(indexPath as NSIndexPath).row].placemark
    handleMapSearchDelegate?.dropPinZoomIn(selectedItem)
    dismiss(animated: true, completion: nil)
}

}

Спасибо за вашу помощь!

...