2017-04-09 23 views
1

我做了一個簡單的函數來執行本地搜索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,它說「不找到地方「每次搜索時

回答

0

問題是你還沒有初始化localSearchResponse你只是聲明它。

改變的線:

var localSearchResponse:MKLocalSearchResponse! 

隨着

var localSearchResponse = MKLocalSearchResponse() 
0

看起來像您在調用該行時訪問錯誤的搜索欄對象。該函數的參數爲​​searchBar: UISearchBar,因此您可能打算使用該變量。更改行與錯誤:

localSearchRequest.naturalLanguageQuery = searchBar.text

看起來你有這個代碼一堆的地方,所以你可能只是想改變函數的參數名稱。

+0

好,這是行不通的。它仍然表示意外地發現了一個零值 –

+0

在更改代碼後,您的故事板是否仍正常連接 –

相關問題