Reputation: 951
How can I set my map MKAnnotation CalloutAccessoryView to show the selected image mapped out from my realm database (with icons and buttons on either side, as the left and right calloutAccessory?). I also need it to recognize the class/member for other functions on my MapViewController here? The mapping of the data and left and right callout accessories are working fine, but I can't figure out how to add the selected image, so it show up as a large poster-style image on the callout (with the small icons & buttons on either side).
The image is SpecimenAnnotation.objectPoster
The SpecimenAnnotation (which is working fine to map the data) and 'objectPoster' value is the selected (and mapped) Annotation from the class Specimen, included in my file Specimen.swift, like this;
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!
}
In my MapViewController, on the line 'annotationView.detailCalloutAccessoryView = UIImageView(image: SpecimenAnnotation.objectPoster)' I am getting a red alert error "instance member 'objectPoster' cannot be used on type 'Specimen Annotation'
I am a beginner. Would greatly appreciate your suggestions. Any ideas?
Here are the code blocks regarding this data and error:
// 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
}
Upvotes: 0
Views: 489
Reputation: 951
I solved it, by pulling the value into the currentAnnotation, and adding it in a UIImageView on the DetailCalloutAccessoryView
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
}
Upvotes: 0