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

标签 ios swift mapkit mklocalsearch

我正在使用以下代码:

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

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

我得到这个: enter image description here 虽然我需要这个: enter image description here

最佳答案

我不是 MapKit 方面的专家,但在阅读文档后,我觉得您可以通过在 中设置 region 参数来指定搜索区域MKLocalSearchRequest。下面的代码是官方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 中的可见区域。因此,如果您获得的是本地结果,可能是因为您的 map 放大了该区域。

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

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

关于ios - 如何制作 - 自动完成搜索全局而不是本地,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52699127/

相关文章:

ios - 将 uiimage 制作成椭圆形时图像被拉伸(stretch)

iphone - mac上的iOS模拟器运行的是i386架构,不是armv7?

ios - Swift 从 didSet 调用委托(delegate)

ios - Swift - 带有图像的自定义 MKPointAnnotation

objective-c - 是否可以将 "Styled Maps"与 MKMapView 一起使用?

ios - 如何快速访问 XCTest 的外部文件

ios - 使用 NSURLSession 和 Queue 上传数据

ios - Swift 枚举和 CBCharacteristicProperties

swift - 缩放以适应 map 上的当前位置和注释

ios - 将字符串转换为日期并获取日期字符串