从Swift 2.0中的“DidSelectAnnotationView”和CoreData中的数据设置callout注释标题

时间:2015-10-20 10:04:20

标签: swift core-data annotations mapkit

请帮忙。 =)我有一个问题来设置标注标题。我从带有谓词的核心数据中收到信息。 当我点击地图上的注释时,我会从我的日志中的引脚接收所有数据。

怎样才能在标注泡泡中设置标题?

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

  let annotationView:MKPinAnnotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "CheckpointPins")

 if annotation is MKUserLocation {
     return nil }
  else {

        let pred:NSPredicate = NSPredicate(format: "checkpoint_longitude != nil && checkpoint_latitude != nil")
        let items = fetchedResultsController?.fetchedObjects
        let filt = (items! as NSArray).filteredArrayUsingPredicate(pred)

        let startCheckPoint:Checkpoint = (filt as NSArray).firstObject as! Checkpoint!
        let endCheckpoint:Checkpoint = (filt as NSArray).lastObject as! Checkpoint!

        if filt.count != 0 {

           if startCheckPoint .isEqual(annotation) {
              annotationView.pinTintColor = MKPinAnnotationView.greenPinColor()
           }else if endCheckpoint.isEqual(annotation) {

              annotationView.pinTintColor = MKPinAnnotationView.purplePinColor()
           }else {
              annotationView.pinTintColor = MKPinAnnotationView.redPinColor()

           }
     }
           let point = annotation as! Checkpoint

           annotationView.tag = (point.checkpoint_order?.integerValue)!
           annotationView.enabled = true
           annotationView.animatesDrop = true
   //  annotationView.canShowCallout = true
   annotationView.annotation = annotation
           // annotationView.opaque = false
           print("tag = \(annotationView.tag)")
  }
  return annotationView
 }

 func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {

  if !(view.annotation!.isKindOfClass(MKUserLocation)) {

     let pred:NSPredicate = NSPredicate(format: "checkpoint_longitude != nil && checkpoint_latitude != nil")
     let pred2:NSPredicate = NSPredicate(format: "checkpoint_order == %d", view.tag)
     let compoundPredicate:NSPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: [pred, pred2])

     let items = fetchedResultsController?.fetchedObjects
     let filt = (items! as NSArray).filteredArrayUsingPredicate(compoundPredicate)
     let point:Checkpoint = (filt as NSArray).firstObject as! Checkpoint

     print(point.description)

        }

  }
 }

1 个答案:

答案 0 :(得分:2)

首先,如果您想要显示标注,则需要将canShowCallout设置为true。除非您启用它们,否则没有标注。

在评论中,您说您遇到了一个错误,其中显示的错误为must implement title when canShowCallout is YES on corresponding view <MKPinAnnotationView: 0x7fb9ef536e70...这告诉您的是,您用作注释的对象未实现title部分MKAnnotation协议。 title是协议的可选部分,除非您使用标注,在这种情况下您必须实施它。标题是标注上显示的内容。错误消息告诉您已启用标注但未提供要显示的标题。您可以将title添加到您正在使用的班级MKAnnotation,也可以跳过使用标注。