我做了一个简单的函数来执行本地搜索
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
var localSearchRequest:MKLocalSearchRequest!
var localSearchResponse:MKLocalSearchResponse!
localSearchRequest.naturalLanguageQuery = locationSearchBar.text
let localSearch = MKLocalSearch(request: localSearchRequest)
localSearch.start { (localSearchResponse, Error) in
if localSearchResponse == nil{
let alertController = UIAlertController(title: nil, message: "No Place Found", preferredStyle: UIAlertControllerStyle.alert)
alertController.addAction(UIAlertAction(title: "확인", style: UIAlertActionStyle.default, handler: nil))
self.present(alertController, animated: true, completion: nil)
return
}
self.pointAnnotation = MKPointAnnotation()
self.pointAnnotation.title = self.locationSearchBar.text
self.pointAnnotation.coordinate = CLLocationCoordinate2D(latitude:localSearchResponse!.boundingRegion.center.latitude, longitude: localSearchResponse!.boundingRegion.center.longitude)
self.pinAnnotationView = MKPinAnnotationView(annotation: self.pointAnnotation, reuseIdentifier: nil)
self.mapView.centerCoordinate = self.pointAnnotation.coordinate
self.mapView.addAnnotation(self.pinAnnotationView.annotation!)
}
}
但它不起作用
它会转到
localSearchRequest.naturalLanguageQuery = locationSearchBar.text
行上的“Thread 1 EXC_BAD_INSTRUCTION”,每次搜索时它都会显示“No Place Found” 最佳答案
问题是您没有初始化您刚刚声明的localSearchResponse
。
更改行:
var localSearchResponse:MKLocalSearchResponse!
与
var localSearchResponse = MKLocalSearchResponse()
关于ios - MapKit LocalSearch无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43302117/