2017-07-01 91 views
0

我正試圖在地圖上顯示針腳,並帶有預設的搜索參數。點擊按鈕後,該應用會顯示所有本地出租車服務。這部分我有...我有項目追加到[MKPointAnnotation],它成功地在tableView以及mapView上顯示一個填充列表。出於某種原因,當您點擊mapView上的引腳時,即使執行viewFor annotation:委託,我也無法獲得標題和副標題。有任何想法嗎?Swift MKMapView針腳註釋問題

class MapViewController: UIViewController, CLLocationManagerDelegate, UITableViewDataSource, UITableViewDelegate, MKMapViewDelegate { 

@IBOutlet weak var mapView: MKMapView! 
@IBOutlet weak var tableView: UITableView! 

let locationManager = CLLocationManager() 
let regionRadius: CLLocationDistance = 1500 
var matchingItems: [MKMapItem] = [MKMapItem]() 
var annotationGroup = [MKPointAnnotation]() 

override func viewDidLoad() { 
     super.viewDidLoad() 
     locationManager.delegate = self 
     tableView.delegate = self 
     tableView.dataSource = self 
     mapView.delegate = self 


     tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell") 
     tableView.allowsSelection = true 


     let currentLocation = locationManager.location 
     guard let latitude = currentLocation?.coordinate.latitude, 
      let longitude = currentLocation?.coordinate.longitude else { 
       alertPopup(title: "Enable Location Services", message: "Navigate to Settings >", buttonTitle: "Ok") 
       return 
     } 
     let initialLocation = CLLocation(latitude: latitude, longitude: longitude) 
     tabBarController?.tabBar.isHidden = true 
     centerMapOnLocation(location: initialLocation) 

     let request = MKLocalSearchRequest() 
     request.naturalLanguageQuery = "Taxi" 
     request.region = mapView.region 

     let search = MKLocalSearch(request: request) 

     search.start(completionHandler: {(response, error) in 

      if error != nil { 
       print("Error occured in search: \(error!.localizedDescription)") 
      } else if response!.mapItems.count == 0 { 
       print("No Matches Found") 
      } else { 
       print("Matches Found") 
      } 

      for location in response!.mapItems { 
       print("Name = \(String(describing: item.name))") 
       print("Phone = \(String(describing: item.phoneNumber))") 
       self.matchingItems.append(item) 

       var pinAnnotationView = MKPinAnnotationView() 
       let annotation = MKPointAnnotation() 
       annotation.coordinate.longitude = location.placemark.coordinate.longitude 
       annotation.coordinate.latitude = location.placemark.coordinate.latitude 
       annotation.title? = location.name! 
       annotation.subtitle = location.phoneNumber! 
       self.annotationGroup.append(annotation) 
       self.mapView.addAnnotations(self.annotationGroup) 
       pinAnnotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: nil) 
       self.mapView.addAnnotation(pinAnnotationView.annotation!) 
      } 
      self.tableView.reloadData() 
      print("Reloaded Table Data") 
      print(self.matchingItems) 
      self.mapView.showAnnotations(self.annotationGroup, animated: true) 

     }) 
    } 

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 

     if !(annotation is MKUserLocation) { 
      let pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: String(annotation.hash)) 

      let rightButton = UIButton(type: .contactAdd) 
      rightButton.tag = annotation.hash 

      pinView.animatesDrop = true 
      pinView.canShowCallout = true 
      pinView.rightCalloutAccessoryView = rightButton 

      return pinView 
     } else { 
      print("Returned nil") 
      return nil 
     } 

    } 
} 
+0

我認爲問題是,你是不是直接添加註釋self.mapView。相反,您將其添加到self.annotationGroup。你不解釋它是什麼。 –

+0

@El Tomato'annotationGroup'是在MapViewController類的開頭聲明的註釋數組,我將註釋附加到循環中。我將註釋數組添加到'mapView' ..'self.mapView.addAnnotations(self.annotationGroup')。 – szady

回答

0

問題是可選的annotation.title? = location.name!。刪除可選,它的作品。

for location in response!.mapItems { 
     print("Name = \(String(describing: location.name))") 
     print("Phone = \(String(describing: location.phoneNumber))") 
     self.matchingItems.append(location) 
     let annotation = MKPointAnnotation() 
     annotation.coordinate.longitude = location.placemark.coordinate.longitude 
     annotation.coordinate.latitude = location.placemark.coordinate.latitude 
     annotation.title = location.name! //This is the line to remove the optional annotation.title? from. 
     annotation.subtitle = location.phoneNumber! 
     self.annotationGroup.append(annotation) 
     self.mapView.addAnnotations(self.annotationGroup) 
     self.mapView.showAnnotations(self.annotationGroup, animated: true) 
     } 
0

也許爲時已晚,但無論如何

首先,MKAnnotation是一個協議,所以我認爲你必須定義一個自定義類的對象,這個類必須實現這個協議或者在這種情況下,我們可以定義MKMapItem一個擴展實施MKAnnotation協議

extension MKMapItem : MKAnnotation 
{ 
    public var coordinate: CLLocationCoordinate2D { 
    return self.placemark.coordinate 
    } 


    // Title and subtitle for use by selection UI. 
    public var title: String? { 
    return self.name 
    } 

    public var subtitle: String? { 
    return self.phoneNumber 
    } 
} 

這個代碼將減少到這

search.start(completionHandler: {(response, error) in 

      if error != nil { 
       print("Error occured in search: \(error!.localizedDescription)") 
      } else if response!.mapItems.count == 0 { 
       print("No Matches Found") 
      } else { 
       print("Matches Found") 
      } 

      for location in response!.mapItems { 
       print("Name = \(String(describing: item.name))") 
       print("Phone = \(String(describing: item.phoneNumber))") 
       self.matchingItems.append(item) 
      } 
      self.mapView.addAnnotations(self.matchingItems) 
      self.tableView.reloadData() 
      print("Reloaded Table Data") 
      print(self.matchingItems) 
      self.mapView.showAnnotations(self.matchingItems, animated: true) 
     }) 

一旦你有了這個那麼你的func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {}實施應該工作

希望這有助於