How can I perform an action when I tap on a pin from MapKit? ios9, swift 2
Asked Answered
Z

3

16

I've created a map with a ton of pins all over it, and tapping a pin pops up the default "bubble" and that's fine, for now.

What I would really like to do is not pop up the bubble at all and instead call a different function. All of my searches have resulted in people wanting to create new custom annotations with different views and such, I just want to call a function and I'm not sure where I would try to call it from. I'm pretty new to ios development and this seems like it should be pretty straightforward, but I'm finding this isn't usually the case.

Zn answered 16/5, 2016 at 5:25 Comment(0)
M
13

First, it's worth noting that the standard UX is to present a callout bubble which shows the user enough information to confirm that this was the intended annotation view and then have the callout include left and/or right accessory views (e.g. buttons on left and/or right side of the callout) which lets the user then perform some additional task(s) using that annotation. See Creating Callouts in the Location and Maps Programming Guide.

Golden Gate Bridge

But if you want to immediately do something else when the user taps on the annotation, then set up your annotation view so that (a) it has no callout; and then (b) implement MKMapViewDelegate method didSelectAnnotationView to perform whatever task you want when the user taps on the annotation view.


For example, assuming you've specified the delegate for your map view, you could do something like the following in Swift 3/4:

private let reuseIdentifier = "MyIdentifier"

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation { return nil }

    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseIdentifier) as? MKPinAnnotationView
    if annotationView == nil {
        annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
        annotationView?.tintColor = .green                // do whatever customization you want
        annotationView?.canShowCallout = false            // but turn off callout
    } else {
        annotationView?.annotation = annotation
    }

    return annotationView
}

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    // do something
}

Or in Swift 2:

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation { return nil }

    var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseIdentifier) as? MKPinAnnotationView
    if annotationView == nil {
        annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
        annotationView?.tintColor = UIColor.greenColor()  // do whatever customization you want
        annotationView?.canShowCallout = false            // but turn off callout
    } else {
        annotationView?.annotation = annotation
    }

    return annotationView
}

func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
    // do something
}
Mesdames answered 16/5, 2016 at 5:51 Comment(0)
S
10

You can stop callout bubble from showing by setting canShowCallout property on MKAnnotationView.

anView!.canShowCallout = false

Second implement didSelectAnnotationView delegate method of MapView to Handle something on pin selection.you can than call your method from didSelectAnnotationView.

func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!)
{
    //Pin clicked, do your stuff here
}
Sunny answered 16/5, 2016 at 5:58 Comment(0)
L
5

Swift 5

override func viewDidLoad() {
   super.viewDidLoad()
   mapView.delegate = self
}

extension MapViewController: MKMapViewDelegate {

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

   func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView){
      print("didSelectAnnotationTapped")
   }
}
Literacy answered 13/11, 2020 at 8:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.