0
votes

I can't seem to workout why the MKMapViewDelegate function is not being called when the annotation on the map is tapped.

Here is my code. Im expecting the very last function calloutAccessoryControlTapped to be called when the pin is tapped. But it isn't working.

class FindPeopleNearbyViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

    var locationManager : CLLocationManager!
    @IBOutlet weak var mapView : MKMapView!

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        determineCurrentLocation()
    }

    func determineCurrentLocation()
    {
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestAlwaysAuthorization()

        if CLLocationManager.locationServicesEnabled() {
            locationManager.startUpdatingLocation()
        }
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

        let userLocation:CLLocation = locations[0] as CLLocation

        let center = CLLocationCoordinate2D(latitude: userLocation.coordinate.latitude, longitude: userLocation.coordinate.longitude)
        let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.0025, longitudeDelta: 0.0025))

        mapView.setRegion(region, animated: true)

        // Drop a pin at user's Current Location
        let myAnnotation: MKPointAnnotation = MKPointAnnotation()
        myAnnotation.coordinate = CLLocationCoordinate2DMake(userLocation.coordinate.latitude, userLocation.coordinate.longitude);
        myAnnotation.title = "My location"

        mapView.addAnnotation(myAnnotation)

        self.locationManager.stopUpdatingLocation()
    }

    internal func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
        print("In annotation calloutAccessoryControlTapped")
    }

Any help appreciate.

1
Did you set your mapview's delegate to self? - Honey
Nope. duh! Thank you, i knew it was something stupid like that. - croc_hunter

1 Answers

1
votes

Check

class FindPeopleNearbyViewController: MKMapViewDelegate {

and

mapView.delegate = self