如何制作 - 自动完成搜索全局而不是本地

How to make - Autocomplete search for global rather than local

我正在使用以下代码:

class SearchViewController: UIViewController {

    var searchCompleter = MKLocalSearchCompleter()
    var searchResults = [MKLocalSearchCompletion]()

    @IBOutlet weak var searchResultsTableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        searchCompleter.delegate = self
    }
}

extension SearchViewController: UISearchBarDelegate {
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        searchCompleter.queryFragment = searchText
    }
}

extension SearchViewController: MKLocalSearchCompleterDelegate {

    func completerDidUpdateResults(_ completer: MKLocalSearchCompleter) {
        searchResults = completer.results
        searchResultsTableView.reloadData()
    }

    func completer(_ completer: MKLocalSearchCompleter, didFailWithError error: Error) {
        // handle error
    }
}

extension SearchViewController: UITableViewDataSource {

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

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let searchResult = searchResults[indexPath.row]
        let cell = UITableViewCell(style: .subtitle, reuseIdentifier: nil)
        cell.textLabel?.text = searchResult.title
        cell.detailTextLabel?.text = searchResult.subtitle
        return cell
    }
}

extension SearchViewController: UITableViewDelegate {

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)

        let completion = searchResults[indexPath.row]

        let searchRequest = MKLocalSearchRequest(completion: completion)
        let search = MKLocalSearch(request: searchRequest)
        search.start { (response, error) in
            let coordinate = response?.mapItems[0].placemark.coordinate
            print(String(describing: coordinate))
        }
    }
}

然而,这仅搜索本地而非全局。如何对全局数据进行自动完成搜索?我无法获得是否有 api for iOS 以及如何实现它。非常感谢帮助。

我得到这个: 虽然我需要这个:

我不是 MapKit 方面的专家,但在阅读文档后我觉得您可以通过在 MKLocalSearchRequest 中设置 region 参数来指定搜索区域。下面的代码是官方docs.

中的例子

When specifying your search strings, include a map region to narrow the search results to the specified geographical area.

let request = MKLocalSearchRequest()
request.naturalLanguageQuery = "coffee"

// Set the region to an associated map view's region
request.region = myMapView.region

myMapView.region 是您 MKMapView 中的可见区域。因此,如果您获得的是本地结果,可能是因为您的地图放大了该区域。

但是,如果您在地图的可见区域之外显示结果,那么这看起来 糟糕的用户体验,因为用户期望在他可见的区域中获得结果.查看当前的地图应用程序都做了什么。它们首先显示可见区域内的结果,然后是其他区域。这似乎是因为他们在文档的最后一行所说的。

Specifying a region does not guarantee that the results will all be inside the region. It is merely a hint to the search engine.

我遇到了同样的问题,我发现 MKLocalSearch.Request (https://developer.apple.com/documentation/mapkit/mklocalsearch/request) works better for global search than MKLocalSearchCompletion. You just have to make sure to make the region the default region, which according to Apple is Global (https://developer.apple.com/documentation/mapkit/mklocalsearchcompleter/1451923-region):

Use this property to limit search results to the specified geographic area. The default value of this property is a region that spans the entire world.

可按如下方式进行:

let request = MKLocalSearch.Request()
request.region = MKCoordinateRegion()

request.naturalLanguageQuery = query
let search = MKLocalSearch(request: request)
  
search.start { response, error in
  guard let response = response else {
    return
   }
    
  self.predictions = response.mapItems
}