brettfazio
brettfazio

Reputation: 1156

Custom MKPointAnnotation isn't responding to user interaction

I am making a Swift application that uses MKPointAnnotations, and I recently ran into an issue where I needed to store metadata in my annotations, so I created the custom class below:

class BRETTFAnnotation: MKPointAnnotation {

    var tag: Int64

    var name: String

    init(lat : Double, lon:Double, t : Int64, n: String) {

        self.tag = t
        self.name = n

        super.init()


        self.coordinate = CLLocationCoordinate2D(latitude: lat, longitude: lon)
    }

}

My MKAnnotationView viewfor MKAnnotation method is shown below:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    let newAnnotation = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "reuse")

    newAnnotation.canShowCallout = true

    let right = self.button(title: "Yes")
    right?.addTarget(self, action: #selector(clickedToConfirmNewPoint), for: .touchUpInside)
    newAnnotation.rightCalloutAccessoryView = right

    let left = self.button(title: "No")
    left?.addTarget(self, action: #selector(clickedToCancelNewPoint), for: .touchUpInside)
    newAnnotation.leftCalloutAccessoryView = left

    return newAnnotation
}

The problem I am running into is when ever I click on my custom BRETTFAnnotation (which I add to my MKMapView) nothing happens. When I was just using the MKPointAnnotation (instead of the BRETTFAnnotation) when I clicked on the map the two buttons on the MKAnnotationView would show. I am trying to get the MKPinAnnotationView to show on touch using my BRETTFAnnotation instead of the MKPointAnnotation.

How can I continue to use my custom annotation and show the callout when the user clicks on the annotation at the same time?

Edit 1: Since it is probably useful the code below is how I make the annotation and add it to the mapView.

        let location = gestureRecognizer.location(in: mapView)
        let coordinate = mapView.convert(location,toCoordinateFrom: mapView)

        print("adding lat,long \(coordinate.latitude),\(coordinate.longitude)")


        lastPoint = BRETTFAnnotation(lat: coordinate.latitude, lon: coordinate.longitude, t: 1, n: "")

        let annotationView = MKPinAnnotationView(annotation: lastPoint, reuseIdentifier: "reuse")

        mapView.addAnnotation(lastPoint)

Upvotes: 0

Views: 738

Answers (2)

kuzdu
kuzdu

Reputation: 7524

When you use your own MKAnnoation you can handle your actions in didSelect. Just implement the following code.

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    if let yourAnnotation = view.annotation as? BRETTFAnnotation {
       //handle your meta data or/and show UIViews or whatever
    }
}

with

func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) {
    //getting called when you tap on map or on another annotation (not the selected annotation before) 
    //hide UIViews or do whatever you want
 }

That does work for me:

class ViewController: UIViewController, MKMapViewDelegate {

    func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {


    print("didSelect")
    if let annoation = view.annotation as? MyAnnoation {
        print("metatag \(annoation.metaTag)")
    }
}

func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) {
    print("didDeselect")
}

override func viewDidLoad() {
    super.viewDidLoad()


    mapView.delegate = self
    let annotation = MyAnnoation(n: "name", m: "metaTag")
    annotation.coordinate = CLLocationCoordinate2D(latitude: 50.0, longitude: 8.0)
    mapView.addAnnotation(annotation)
   }
 }



class MyAnnoation: MKPointAnnotation {


var name: String?
var metaTag: String?


init(n: String, m: String) {
    self.name = n
    self.metaTag = m
  }
}

Upvotes: 0

brettfazio
brettfazio

Reputation: 1156

I fix this problem by making my BRETTFAnnotation a subclass of NSObject and MKAnnotation instead of MKPointAnnotation. Doing this allowed my custom class to receive user interaction and show the callouts.

Upvotes: 0

Related Questions