Hello,
I've added two buttons for my annotationView. I have 25 annotations and I would like to have seperate button actions for each annotation. Now I have only one button action for every annotation. Here is my code:
import UIKit
import MapKit
class ViewController: UIViewController, MKMapViewDelegate {
let initialLocation = CLLocation(latitude: 85.10786543576327, longitude: 11.03851472106171)
let locationManager = CLLocationManager()
struct Place {
let name: String
let description: String
let type: String
let lattitude: CLLocationDegrees
let longtitude: CLLocationDegrees
var coordinate: CLLocationCoordinate2D {
.init(latitude: lattitude, longitude: longtitude)
}
}
let location = CLLocation()
let places = [Place(name: "One", description: "One", type: "one", lattitude: 81.108187, longtitude: 12.075812),
Place(name: "Two", description: "Two", typ: "two", lattitude: 81.076187, longtitude: 11.000563),
Place(name: "Three", description: "Three", typ: "Three", lattitude: 81.076187, longtitude: 11.000563)]
[...]
func findPlace(_ miejsca: [Place]) {
for place in places {
let annotations = MKPointAnnotation()
annotations.title = place.name
annotations.subtitle = place.description
annotations.coordinate = CLLocationCoordinate2D(latitude:
place.lattitude, longitude: place.longtitude)
mapView.addAnnotation(annotations)
}
}
@objc func didClickDetailDisclosure(button: UIButton) {
guard let vc = storyboard?.instantiateViewController(withIdentifier: "kopuly_vc") as? KopulyController else {
return
}
present(vc, animated: true)
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard !(annotation is MKUserLocation) else { return nil }
mapView.delegate = self
let annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: String(annotation.hash))
let identifier = "identifier"
annotationView.canShowCallout = true
guard let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier, for: annotation) as? MKMarkerAnnotationView else { return nil }
let rightButton = UIButton(type: .detailDisclosure) // HERE IS THIS BUTTON, BUT I NEED EACH BUTTON FOR EACH ANNOTATION - THIS WAY IT SHOULD BE ONE FOR ALL OF THEM
rightButton.tag = annotation.hash
annotationView.canShowCallout = true
annotationView.rightCalloutAccessoryView = rightButton
rightButton.addTarget(self, action: #selector(didClickDetailDisclosure(button:)), for: .touchUpInside)
let leftButton = UIButton(frame: CGRect(
origin: CGPoint.zero,
size: CGSize(width: 25, height: 25)))
leftButton.setBackgroundImage(#imageLiteral(resourceName: "nav"), for: .normal)
annotationView.leftCalloutAccessoryView = leftButton
leftButton.addTarget(self, action: #selector(didClickDetailDisclosure(button:)), for: .touchUpInside)
[...]
Thank you in advance for your help
What do you mean by setting a tag to each annotation?
Unfortunately, you cannot add the tag directly.
What I did was to use the title to add this information. Either with an explicit number (at the beginning or at the end). That's what you do if you give title as One, Two… I used a trick to add sequence of invisible characters (space and optionSpace)
You could modify findPlace for this:
func findPlace(_ miejsca: [Place]) {
for (i, place) in places.enumerated() {
let annotations = MKPointAnnotation()
annotations.title = String(i) + "-" + place.name // initial chars before "-" give the "tag"
annotations.subtitle = place.description
annotations.coordinate = CLLocationCoordinate2D(latitude:
place.lattitude, longitude: place.longtitude)
mapView.addAnnotation(annotations)
}
}
Then, in
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
You retrieve this Int at the beginning to use in the switch case.
You do not show the different didClickDetailDisclosure.
You could have:
// for VC1
@objc func didClickDetailDisclosure1(button: UIButton) {
guard let vc = storyboard?.instantiateViewController(withIdentifier: "kopuly_vc1") as? KopulyController else { return }
present(vc, animated: true)
}
// for VC2
@objc func didClickDetailDisclosure2(button: UIButton) {
guard let vc = storyboard?.instantiateViewController(withIdentifier: "kopuly_vc2") as? KopulyController else { return }
present(vc, animated: true)
}
Hope that's clear.