我做了一個簡單的函數來執行本地搜索MapKit本地搜索無法正常工作
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!)
}
}
,但它不工作
它變爲「線程1 EXC_BAD_INSTRUCTION」上線localSearchRequest.naturalLanguageQuery = locationSearchBar.text
,它說「不找到地方「每次搜索時
好,這是行不通的。它仍然表示意外地發現了一個零值 –
在更改代碼後,您的故事板是否仍正常連接 –