so i am setting annotations on a map using a for-loop:
for location in locations {
let annotation = customAnnotation()
annotation.title = location.title
annotation.subtext= location.subtext
annotation.coordinate = CLLocationCoordinate2D (latitude: location.latitude, longitude: location.longitude)
map.addAnnotation(annotation)
}
self.map.delegate = self
and i am trying to print a subtext inside a callout, which is different for every annotation.
I am using this to describe the annotations properties:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard let annot = annotation as? customAnnotation else { return nil }
if annotation is MKUserLocation {
return nil
}
let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "customAnnotation")
annotationView.image = UIImage(named: annot.icon)
annotationView.canShowCallout = true
let rightButton = UIButton(type: .infoLight)
rightButton.setImage(UIImage(named: annot.isCollapsed ? "ic_showmore" : "ic_showless"), for: .normal)
annotationView.rightCalloutAccessoryView = rightButton
if (annot.isCollapsed) {
annotationView.detailCalloutAccessoryView = nil
}
else {
print(annot.subtext)
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 21))
label.text = "text"
label.font = UIFont.italicSystemFont(ofSize: 14.0)
label.numberOfLines = 0
annotationView.detailCalloutAccessoryView = label
label.widthAnchor.constraint(lessThanOrEqualToConstant: label.frame.width).isActive = true
label.heightAnchor.constraint(lessThanOrEqualToConstant: 90.0).isActive = true
}
return annotationView
}
the if-else-part defines the annotations callout.
Now my problem is that the "label.text" is actually printed inside the callout, but only when its an actual text (example above: label.text = "text"). As soon as i try to fill in "annot.subtext", so there is a different text for every annotation, it shows a blank space.
When debugging i started putting print-statements in there (as seen in the code above just below the "else"). it printed my "annot.subtext" when i put the print-statement inside the if-part. But when i put "print(annot.subtext)" inside the else-part, it just showed me an error "CUICatalog invalid asset name supplied: ' ' ".
(the class i used to define my annotation:
class customAnnotation: MKPointAnnotation {
var isCollapsed = true
var setNeedsToggle = false
var subtext: String = " "
var icon: String = " "
Hope one of you can find a solution. Of course i am open for any questions.
Thanks a lot,
Gus