2017-04-25 43 views
0

我有一個地圖搜索控制器,當我搜索一些註釋時,它會縮放它。 此外,我想,當我按下因此,它會放大正確的PIN碼,並自動打開特定註釋AnnotationView在搜索時打開

這裏的annotationView是我的代碼:

https://github.com/hellzkitchen/stackoverflow-041917.git

import UIKit 
import MapKit 

class LocationSearchTable : UITableViewController { 

    var matchingItems = [CustomAnnotations]() 
    var mapView: MKMapView? = nil 

    var handleMapSearchDelegate:HandleMapSearch? = nil 

    func parseAddress(selectedItem:MKPlacemark) -> String { 
     // put a space between "4" and "Melrose Place" 
     let firstSpace = (selectedItem.subThoroughfare != nil && selectedItem.thoroughfare != nil) ? " " : "" 
     // put a comma between street and city/state 
     let comma = (selectedItem.subThoroughfare != nil || selectedItem.thoroughfare != nil) && (selectedItem.subAdministrativeArea != nil || selectedItem.administrativeArea != nil) ? ", " : "" 
     // put a space between "Washington" and "DC" 
     let secondSpace = (selectedItem.subAdministrativeArea != nil && selectedItem.administrativeArea != nil) ? " " : "" 
     let addressLine = String(
      format:"%@%@%@%@%@%@%@", 
      // street number 
      selectedItem.subThoroughfare ?? "", 
      firstSpace, 
      // street name 
      selectedItem.thoroughfare ?? "", 
      comma, 
      // city 
      selectedItem.locality ?? "", 
      secondSpace, 
      // state 
      selectedItem.administrativeArea ?? "" 
     ) 
     return addressLine 
    } 

    func search(keywords:String) { 
     self.matchingItems.removeAll() 
     for annotation in self.mapView!.annotations { 
      if annotation.isKindOfClass(CustomAnnotations) { 
       //Just an example here for searching annotation by title, you could add other filtering actions else. 
       if (annotation.title??.rangeOfString(keywords) != nil) { 
        self.matchingItems.append(annotation as! CustomAnnotations) 
       } 
      } 
     } 
     self.tableView.reloadData() 
    } 

} 

extension LocationSearchTable : UISearchResultsUpdating { 
    func updateSearchResultsForSearchController(searchController: UISearchController) { 
     guard let mapView = mapView, 
      let searchBarText = searchController.searchBar.text else { return } 
     let request = MKLocalSearchRequest() 
     request.naturalLanguageQuery = searchBarText 
     request.region = mapView.region 
     let search = MKLocalSearch(request: request) 
     search.startWithCompletionHandler { response, _ in 
      guard let response = response else { 
       return 
      } 
      self.matchingItems = response.mapItems 
      self.tableView.reloadData() 
     } 
    } 

} 

extension LocationSearchTable { 
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return matchingItems.count 
    } 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("MapSearchCell", forIndexPath: indexPath) 
     let selectedItem = matchingItems[indexPath.row] 
     cell.textLabel?.text = selectedItem.title 
     cell.detailTextLabel?.text = selectedItem.subtitle 
     return cell 
    } 

} 


extension LocationSearchTable { 
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
     let selectedItem = matchingItems[indexPath.row]//.placemark 
     handleMapSearchDelegate?.dropPinZoomIn(selectedItem) 
     dismissViewControllerAnimated(true, completion: nil) 
    } 
} 

回答

1

你之後搜索指定的註釋,有一個方法selectAnnotation: 所以當你點擊那個結果時,使用

mapView.selectAnnotation(resultAnnotation, animated: true) 
+0

當我回家時我會檢查它。我需要把它放到我的mapviewcontroller中? –

+0

工作!謝謝 ! –