i have a lot of annotations on a map and a link inside each annotationcallout, which forwards the user to the native mapsapp for a navigation process.
i created my link in here:
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: .detailDisclosure)
rightButton.setImage(UIImage(named: annot.isCollapsed ? "ic_showmore" : "ic_showless"), for: .normal)
annotationView.rightCalloutAccessoryView = rightButton
if (annot.isCollapsed) {
annotationView.detailCalloutAccessoryView = nil
}
else {
//create NavigationButton
let naviButton = UIButton(type: UIButtonType.custom) as UIButton
naviButton.setTitle(annot.address, for: .normal)
naviButton.setTitleColor(.black, for: .normal)
naviButton.titleLabel?.numberOfLines = 0
naviButton.titleLabel?.font = UIFont.italicSystemFont(ofSize: 14.0)
naviButton.frame = CGRect(x: 0, y: 0, width: 200, height: 21)
naviButton.backgroundColor = .white
naviButton.addTarget(self, action: #selector(naviButtonAction), for: .touchUpInside)
annotationView.detailCalloutAccessoryView = naviButton
naviButton.widthAnchor.constraint(lessThanOrEqualToConstant: naviButton.frame.width).isActive = true
naviButton.heightAnchor.constraint(lessThanOrEqualToConstant: 90.0).isActive = true
}
return annotationView
}
and my "naviButtonAction" here:
@objc func naviButtonAction(sender: UIButton) {
let latitude: CLLocationDegrees = 48.147989
let longitude: CLLocationDegrees = 11.617287
let regionDistance: CLLocationDistance = 1000;
let coordinates = CLLocationCoordinate2DMake(latitude, longitude)
let regionSpan = MKCoordinateRegionMakeWithDistance(coordinates, regionDistance, regionDistance)
let options = [MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: regionSpan.center), MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: regionSpan.span)]
let placemark = MKPlacemark(coordinate: coordinates)
let mapItem = MKMapItem(placemark: placemark)
mapItem.name = "B"
mapItem.openInMaps(launchOptions: options)
}
now i want the coordinates and the title to change depending on the annotation the user has clicked. The question is just how?
i tried a little bit and never got a useful result, so i looked if my @objc func naviButtonAction can actually view the coordinates and title and tried printing them, but all it returned was "nil".
The title, coordinates, etc. where created for each annotation inside this for-loop in my override func viewDidLoad:
for location in locations {
let annotation = customAnnotation()
annotation.title = location.title
annotation.address = location.address
annotation.oHour = location.oHour
annotation.oMin = location.oMin
annotation.cHour = location.cHour
annotation.cMin = location.cMin
annotation.coordinate = CLLocationCoordinate2D (latitude: location.latitude, longitude: location.longitude)
Any ideas why i cant call the title and how i can solve this?
Thanks a lot,
Gus