2016-08-23 21 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 

}