2015-11-24 51 views
0

我有一張地圖,並在地圖上ive做了一個搜索欄。你可以寫出你想要的東西,它會找到那個地方並在其中添加註釋。事情是我想顯示一個tableview,它會在我寫完後給我結果。因爲現在它只是猜測並採取它認爲是正確的,這是錯誤的。那麼,如何根據我在搜索欄中編寫的內容顯示不同的建議?有人知道嗎?使用我的搜索欄的代碼IM是向下跌破在桌面視圖中提供基於MKmapview搜索的建議

func searchBarSearchButtonClicked(searchBar: UISearchBar){ 
    //1 
    searchBar.resignFirstResponder() 
    dismissViewControllerAnimated(true, completion: nil) 
    if self.mapView.annotations.count != 0{ 
     annotation = self.mapView.annotations[0] 
     self.mapView.removeAnnotation(annotation) 
    } 
    //2 
    localSearchRequest = MKLocalSearchRequest() 
    localSearchRequest.naturalLanguageQuery = searchBar.text 
    localSearch = MKLocalSearch(request: localSearchRequest) 
    localSearch.startWithCompletionHandler { (localSearchResponse, error) -> Void in 

     if localSearchResponse == nil{ 
      let alertController = UIAlertController(title: nil, message: "Place Not Found", preferredStyle: UIAlertControllerStyle.Alert) 
      alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default, handler: nil)) 
      self.presentViewController(alertController, animated: true, completion: nil) 
      return 
     } 
     //3 
     self.pointAnnotation = MKPointAnnotation() 
     self.pointAnnotation.title = searchBar.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!) 
     self.mapItemData = localSearchResponse?.mapItems.last 
    } 
} 

回答

0

只需枚舉MKMapItems在localSearchResponse並添加這些到實現代碼如下

for item in localSearchResponse!.mapItems { 
    // add to a tableView 
} 

目前你只是抓住了最後mapItem在列表中顯示一個註釋。

+0

所以不是self.mapItemData = localSearchResponse?.mapItems.last的我只需添加一個新的場景到故事板用的tableview和使用在localSearchResponse項目?.mapItems { 這裏的表視圖 } – Tim

+0

正確的。或者,如果有意義的話,你可以在同一個ViewController上添加一個tableView和MapView。 –

+0

是的。大概。我不想將新視圖添加到舊視圖的頂部。必須弄清楚如何做到這一點。感謝Jason的幫助。 – Tim