Моя таблица locationSearchTable не всегда дает правильные результаты ... почему? - PullRequest
0 голосов
/ 06 ноября 2018

Итак, у меня есть это навигационное приложение, в котором пользователь может искать местоположение в таблице поиска. Если они наберут в магазине, таком как Mcdonald's, Target и т. Д., Он всегда будет отображаться правильно. Однако, если вы введете конкретный адрес, он не будет отображаться. Вот мой код tableView для locationSearchTable:

import UIKit
import MapKit

class LocationSearchTable : UITableViewController {
    var handleMapSearchDelegate:handleMapSearch? = nil
    var matchingItems:[MKMapItem] = []
    var mapView: MKMapView? = nil
    var resultSearchController:UISearchController? = nil

    func parseAddress(selectedItem: MKPlacemark) -> String {
        // put a space between "4" and "Melrose Place"
        let firstSpace = (selectedItem.subThoroughfare != nil && selectedItem.thoroughfare != nil) ? " " : ""
        // put a comma between street and city/state
        let comma = (selectedItem.subThoroughfare != nil || selectedItem.thoroughfare != nil) && (selectedItem.subAdministrativeArea != nil || selectedItem.administrativeArea != nil) ? ", " : ""
        // put a space between "Washington" and "DC"
        let secondSpace = (selectedItem.subAdministrativeArea != nil && selectedItem.administrativeArea != nil) ? " " : ""
        let addressLine = String(
            format:"%@%@%@%@%@%@%@",
            // street number
            selectedItem.subThoroughfare ?? "",
            firstSpace,
            // street name
            selectedItem.thoroughfare ?? "",
            comma,
            // city
            selectedItem.locality ?? "",
            secondSpace,
            // state
            selectedItem.administrativeArea ?? ""
        )

        return addressLine
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

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

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "searchCell") as! ResultsDisplayCell

        let selectedIndex = self.matchingItems[indexPath.row]

        cell.titleLabel?.text = selectedIndex.name
        cell.detailTextLabel?.text = selectedIndex.placemark.title
        //cell.detailTextLabel?.text =
        //cell.detailTextLabel?.text = selectedIndex.name
        //cell.detailTextLabel?.text = selectedIndex.placemark.subtitle
        return cell
    }
}

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 { response, _ in
            guard let response = response else {
                return
            }

            for mapItems in response.mapItems{
                self.matchingItems.append(mapItems as MKMapItem)
                print("thisisthearray:\(self.matchingItems)")

                self.matchingItems = response.mapItems
                self.tableView.reloadData()
            }
        }
    }
}

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

Я не совсем уверен, почему он не может найти адреса правильно. Может быть, я неправильно ссылаюсь на функцию parseAddress?

Вот ссылка на GitHub: https://github.com/derekvandermark/VHNavigationAppOfficial-v-0.93

...