2016-11-15 50 views
2

我也跟着how-to-search-for-location-using-apples-mapkit關於mapView 及其在世界各地的搜索與搜索MKLocalSearchannotations地圖標註搜索

不過,我不想與MKLocalSearch搜索,但搜索我自己annotations我加入自己喜歡這些,例如:

let LitzmanLocation = CLLocationCoordinate2DMake(32.100668,34.775192) 
     // Drop a pin 
     let Litzman = MKPointAnnotation() 
     Litzman.coordinate = LitzmanLocation 
     Litzman.title = "Litzman Bar" 
     Litzman.subtitle = "נמל תל אביב 18,תל אביב" 
     mapView.addAnnotation(Litzman) 

     let ShalvataLocation = CLLocationCoordinate2DMake(32.101145,34.775163) 
     // Drop a pin 
     let Shalvata = MKPointAnnotation() 
     Shalvata.coordinate = ShalvataLocation 
     Shalvata.title = "Shalvata" 
     Shalvata.subtitle = "האנגר 28,נמל תל אביב" 
     mapView.addAnnotation(Shalvata) 


     let MarkidLocation = CLLocationCoordinate2DMake(32.074961,34.781679) 
     // Drop a pin 
     let Markid = MKPointAnnotation() 
     Markid.coordinate = MarkidLocation 
     Markid.title = "Markid" 
     Markid.subtitle = "אבן גבירול 30,תל אביב" 
     mapView.addAnnotation(Markid) 

這裏是我的代碼:

MapViewController.Swift:

import UIKit 
import MapKit 
import CoreLocation 

protocol HandleMapSearch { 
    func dropPinZoomIn(placemark:MKPlacemark) 
} 

class MapViewController: UIViewController,MKMapViewDelegate, CLLocationManagerDelegate,UISearchBarDelegate{ 


    @IBOutlet var mapView: MKMapView! 

    var resultSearchController:UISearchController? = nil 

    var selectedPin:MKPlacemark? = nil 

    @IBAction func MapSearchController(sender: AnyObject) { 
     resultSearchController!.hidesNavigationBarDuringPresentation = false 
     self.resultSearchController!.searchBar.delegate = self 
     presentViewController(resultSearchController!, animated: true, completion: nil) 
     self.resultSearchController!.searchBar.barTintColor = UIColor.blackColor() 
     self.resultSearchController!.searchBar.placeholder = "חפש ברים" 
     self.resultSearchController!.dimsBackgroundDuringPresentation = true 
     self.resultSearchController!.searchBar.sizeToFit() 
    } 
override func viewDidLoad() { 
     super.viewDidLoad() 

     let locationSearchTable = storyboard!.instantiateViewControllerWithIdentifier("LocationSearchTable") as! LocationSearchTable 
     resultSearchController = UISearchController(searchResultsController: locationSearchTable) 
     resultSearchController?.searchResultsUpdater = locationSearchTable 

     locationSearchTable.mapView = mapView 

     locationSearchTable.handleMapSearchDelegate = self 
} 
} 
} 

extension MapViewController: HandleMapSearch { 
    func dropPinZoomIn(placemark:MKPlacemark){ 
     // cache the pin 
     selectedPin = placemark 
     // clear existing pins 
     mapView.removeAnnotations(mapView.annotations) 
     let annotation = MKPointAnnotation() 
     annotation.coordinate = placemark.coordinate 
     annotation.title = placemark.name 
     if let city = placemark.locality, 
      let state = placemark.administrativeArea { 
      annotation.subtitle = "(city) (state)" 
     } 
     mapView.addAnnotation(annotation) 
     let span = MKCoordinateSpanMake(0.05, 0.05) 
     let region = MKCoordinateRegionMake(placemark.coordinate, span) 
     mapView.setRegion(region, animated: true) 
    } 
} 

LocalSearchTable.Swift:

import UIKit 
import MapKit 

class LocationSearchTable : UITableViewController { 

    var matchingItems:[MKMapItem] = [] 
    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 
    } 

    } 

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")! 
     let selectedItem = matchingItems[indexPath.row].placemark 
     cell.textLabel?.text = selectedItem.name 
     cell.detailTextLabel?.text = parseAddress(selectedItem) 
     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) 
    } 
} 
+0

的[在圖形頁面搜索註釋]可能的複製(http://stackoverflow.com/questions/40539590/searching-annotations-in-mapview) – Kuba

+0

是的,我想你鏈接到這裏 –

+0

是我知道了,之前沒有看到它,對不起。 – Kuba

回答

1

所以創建註釋對象的數組,並將其保存在一個實例變量。我們稱之爲annotationsArray

然後,您可以設計一個用戶界面,讓用戶輸入搜索詞。假設你只搜索標題。然後,你可以使用:

let titleMatches = annotationsArray.filter{$0.title == titleToMatch} 

,然後顯示一組過濾註釋與addAnnotations()功能的地圖。 (注意是複數)

+0

嘿,謝謝你的回答。你可以請代碼更詳細的代碼,因爲我是絕對新的迅速,我不明白。 –

+0

不知道如何在沒有爲您編寫應用程序的情況下進入更多細節。你知道如何創建一個數組嗎?你知道如何在數組上使用過濾器功能嗎?你知道如何從用戶那裏收集輸入字符串嗎?我建議你依次研究這些事情,並嘗試將它們放在一起。 –

+0

做什麼都可以,任何事情都將是有益的 –