MapKit SearchResultsUpdating 未调用
MapKit SearchResultsUpdating Not called
有人可以查看我的代码吗?我正在尝试使用 MapKit 获取本地搜索结果。我使用了这个教程:https://www.thorntech.com/2016/01/how-to-search-for-location-using-apples-mapkit/
与教程和我所做的不同之处在于我不想显示 mapView。我只想单击按钮,然后 searchController 会弹出,让我可以搜索位置。我遇到的问题是没有调用 SearchResultsUpdating。抱歉,代码很长,这是我第一次使用 MapKit 本地搜索,我不知道我错过了什么。
*编辑已修复。附上修改后的代码。
import UIKit
import MapKit
import CoreLocation
class LocationSearchTable: UITableViewController {
let cellId = "cellId"
var searchController = UISearchController(searchResultsController: nil)
var matchingItems: [MKMapItem] = []
let mapView = MKMapView()
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
setupLocationManager()
setupSearchBar()
setupMapView()
tableView.delegate = self
tableView.dataSource = self
tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
navigationController?.isNavigationBarHidden = false
navigationItem.hidesSearchBarWhenScrolling = false
}
fileprivate func setupLocationManager() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.requestLocation()
}
fileprivate func setupSearchBar() {
let searchBar = searchController.searchBar
searchBar.placeholder = "Enter Location"
navigationItem.searchController = searchController
searchController.searchResultsUpdater = self
definesPresentationContext = true
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
let selectedItem = matchingItems[indexPath.row].placemark
cell.textLabel?.text = selectedItem.name
cell.detailTextLabel?.text = ""
return cell
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return matchingItems.count
}
}
extension LocationSearchTable: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status == .authorizedWhenInUse {
locationManager.requestLocation()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.first {
let span = MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05)
let region = MKCoordinateRegion(center: location.coordinate, span: span)
mapView.setRegion(region, animated: true)
print("location: \(location)")
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print(error)
}
}
extension LocationSearchTable: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
if let searchText = searchController.searchBar.text {
let request = MKLocalSearch.Request()
request.naturalLanguageQuery = searchText
request.region = mapView.region
let search = MKLocalSearch(request: request)
search.start { (response, _) in
guard let response = response else { return }
self.matchingItems = response.mapItems
self.tableView.reloadData()
}
}
}
}
我们尝试让 searchController
先工作。
按照此设置 searchController
- 声明搜索控制器,你知道了。
let searchController = UISearchController(searchResultsController: nil)
- 在
viewDidLoad()
navigationItem.searchController = searchController
searchController.searchResultsUpdater = self
definesPresentationContext = true
- 设置搜索控制器文本更新,确保你的控制器符合
UISearchResultsUpdating
协议
extension LocationSearchTable: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
if let searchText = searchController.searchBar.text {
print(searchText)
}
}
}
当我们使用UISearchController
时,我们不需要设置searchBar
。
下面的评论让我知道搜索文本是否可以打印出来。那我们就可以继续了。
有人可以查看我的代码吗?我正在尝试使用 MapKit 获取本地搜索结果。我使用了这个教程:https://www.thorntech.com/2016/01/how-to-search-for-location-using-apples-mapkit/
与教程和我所做的不同之处在于我不想显示 mapView。我只想单击按钮,然后 searchController 会弹出,让我可以搜索位置。我遇到的问题是没有调用 SearchResultsUpdating。抱歉,代码很长,这是我第一次使用 MapKit 本地搜索,我不知道我错过了什么。
*编辑已修复。附上修改后的代码。
import UIKit
import MapKit
import CoreLocation
class LocationSearchTable: UITableViewController {
let cellId = "cellId"
var searchController = UISearchController(searchResultsController: nil)
var matchingItems: [MKMapItem] = []
let mapView = MKMapView()
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
setupLocationManager()
setupSearchBar()
setupMapView()
tableView.delegate = self
tableView.dataSource = self
tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
navigationController?.isNavigationBarHidden = false
navigationItem.hidesSearchBarWhenScrolling = false
}
fileprivate func setupLocationManager() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.requestLocation()
}
fileprivate func setupSearchBar() {
let searchBar = searchController.searchBar
searchBar.placeholder = "Enter Location"
navigationItem.searchController = searchController
searchController.searchResultsUpdater = self
definesPresentationContext = true
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
let selectedItem = matchingItems[indexPath.row].placemark
cell.textLabel?.text = selectedItem.name
cell.detailTextLabel?.text = ""
return cell
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return matchingItems.count
}
}
extension LocationSearchTable: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status == .authorizedWhenInUse {
locationManager.requestLocation()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.first {
let span = MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05)
let region = MKCoordinateRegion(center: location.coordinate, span: span)
mapView.setRegion(region, animated: true)
print("location: \(location)")
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print(error)
}
}
extension LocationSearchTable: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
if let searchText = searchController.searchBar.text {
let request = MKLocalSearch.Request()
request.naturalLanguageQuery = searchText
request.region = mapView.region
let search = MKLocalSearch(request: request)
search.start { (response, _) in
guard let response = response else { return }
self.matchingItems = response.mapItems
self.tableView.reloadData()
}
}
}
}
我们尝试让 searchController
先工作。
按照此设置 searchController
- 声明搜索控制器,你知道了。
let searchController = UISearchController(searchResultsController: nil)
- 在
viewDidLoad()
navigationItem.searchController = searchController
searchController.searchResultsUpdater = self
definesPresentationContext = true
- 设置搜索控制器文本更新,确保你的控制器符合
UISearchResultsUpdating
协议
extension LocationSearchTable: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
if let searchText = searchController.searchBar.text {
print(searchText)
}
}
}
当我们使用UISearchController
时,我们不需要设置searchBar
。
下面的评论让我知道搜索文本是否可以打印出来。那我们就可以继续了。