2016-08-23 67 views
0

如何设置我的地图MKAnnotation CalloutAccessoryView以显示从我的领域数据库中映射出的选定图像(图标和按钮位于左侧和右侧calloutAccessory? )。我也需要它在这里识别我的MapViewController上的其他函数的类/成员?数据和左右标注附件的映射工作正常,但我无法弄清楚如何添加所选图像,因此它在标注中显示为大型海报式图像(带有小图标&按钮在任何一方)。如何在MKAnnotation中显示所选图像CalloutAccessoryView,swift 2.2

的图像是SpecimenAnnotation.objectPoster

的SpecimenAnnotation(其工作正常将数据映射)和“objectPoster”值是从类样品,包括在我的文件标本选择的(和映射)诠释。迅速,像这样;

class Specimen: Object{ 
    dynamic var name = "" 
dynamic var objectPoster = "" 
dynamic var latitude = 0.0 
dynamic var longitude = 0.0 
dynamic var created = NSDate() 
dynamic var category: Category! 
} 

在我的MapViewController,就行了“annotationView.detailCalloutAccessoryView =的UIImageView(图片:SpecimenAnnotation.objectPoster)”我得到一个红色警报错误“的实例成员‘objectPoster’不能在类型使用的‘样品注释’

我是一个初学者将非常感谢您的建议,任何想法

以下是关于这个数据和错误的代码块:?

 // Create annotations for each one 
    for specimen in specimens { // 3 
     let coord = CLLocationCoordinate2D(latitude: specimen.latitude, longitude: specimen.longitude); 

     let specimenAnnotation = SpecimenAnnotation(coordinate: coord, poster: specimen.objectPoster, subtitle: specimen.category.name, specimen: specimen) 

     mapView.addAnnotation(specimenAnnotation) // 4 
    } 
    } 

} 

//MARK: - MKMapview Delegate 
extension MapViewController: MKMapViewDelegate { 

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

    guard let subtitle = annotation.subtitle! else { return nil } 

    if (annotation is SpecimenAnnotation) { 
     if let annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(subtitle) { 
      return annotationView 
     } else { 

      let currentAnnotation = annotation as! SpecimenAnnotation 

      let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: subtitle) 
      annotationView.image = UIImage(named:"IconStudent") 
      annotationView.enabled = true 
      annotationView.canShowCallout = true      

      // ERROR message here> 
      annotationView.detailCalloutAccessoryView = UIImageView(image: SpecimenAnnotation.objectPoster) 

      // right accessory view 
      let infoButton = UIButton(type: UIButtonType.InfoDark) 
      // Left accessory view 
      let image = UIImage(named: "button50") 
      let specimenButton = UIButton(type: .Custom) 
      specimenButton.frame = CGRectMake(0, 0, 30, 30) 
      specimenButton.setImage(image, forState: .Normal) 
      annotationView.rightCalloutAccessoryView = infoButton 
      annotationView.leftCalloutAccessoryView = specimenButton 

      if currentAnnotation.title == "Empty" { 
       annotationView.draggable = true 
      }    
      return annotationView 
     } 
    } 
    return nil 

} 

回答

0

我解决了它,将值拉入currentAnnotation中,并将其添加到DetailCalloutAccessoryView上的UIImageView中

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

    guard let subtitle = annotation.subtitle! else { return nil } 

    if (annotation is SpecimenAnnotation) { 
     if let annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(subtitle) { 
      return annotationView 
     } else { 

      let currentAnnotation = annotation as! SpecimenAnnotation 

      let currentImage = currentAnnotation.objectPoster! 


      let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: subtitle) 
      } 

      annotationView.enabled = true 
      annotationView.canShowCallout = true 

      // right accessory view 
      let calloutInfoButton = UIButton(type: UIButtonType.InfoDark) 

      // Left accessory view 
      let calloutLeftImage = UIImage(named: "button50p") 
      let calloutLeftButton = UIButton(type: .Custom) 
      calloutLeftButton.frame = CGRectMake(0, 0, 30, 30) 
      calloutLeftButton.setImage(calloutLeftImage, forState: .Normal) 

      //show Image on callout Accessory 

      let url = NSURL(string: currentImage) 
      let data = NSData(contentsOfURL: url!) //make sure your image in this url does exist, otherwise unwrap in a if let check 
      //annotationView.image = UIImage(data: data!) 
      let calloutImage = UIImage(data:data!) 

      annotationView.detailCalloutAccessoryView = UIImageView(image: calloutImage) 

      annotationView.rightCalloutAccessoryView = calloutInfoButton 
      annotationView.leftCalloutAccessoryView = calloutLeftButton 

      if currentAnnotation.title == "Empty" { 
       annotationView.draggable = true 
      } 
      return annotationView 
     } 
    } 
    return nil 

}