Search code examples
iosswift3mkmapviewmkannotation

How to add different button action to the second annotation /pin in map view using swift2


Can any one help me to add different button action to the second annotation /pin (annotation2), Now the button do the same work in the two annotation pins how to do different work to each other . I'am using Swift3 in my project and this is my code . thanks

This is my code.

  import UIKit
  import MapKit
  import CoreLocation

 class MyAnnotation: MKPointAnnotation {
      var uniqueId: Int!
 }

  class LocationViewController: UIViewController , MKMapViewDelegate , CLLocationManagerDelegate{

    @IBOutlet weak var map: MKMapView!{

    didSet{
        map.delegate = self
    }
}

@IBOutlet weak var locationInfo: UITextView!


override func viewDidLoad() {
    super.viewDidLoad()


    let locations = CLLocationCoordinate2DMake(33.314627, 44.303500)

    let location2 = CLLocationCoordinate2DMake(33.312149, 44.3024567)

    let span = MKCoordinateSpanMake(0.02, 0.02)

    let span2 = MKCoordinateSpanMake(0.02, 0.02)

    let region = MKCoordinateRegionMake(locations, span)

     let region2 = MKCoordinateRegionMake(location2, span2)


    map.setRegion(region, animated: true)

    map.setRegion(region2, animated: true)


    let annotation = MyAnnotation()
    //annotation.setCoordinate(location)
    annotation.coordinate = locations
    annotation.title = "Zaid Homes"
    annotation.subtitle = "Hay aljameaa"

    annotation.uniqueId = 1

    map.addAnnotation(annotation)

    let annotation2 = MyAnnotation()
    //annotation.setCoordinate(location)
    annotation2.coordinate = location2
    annotation2.title = "Zaid "
    annotation2.subtitle = "aljameaa"

    annotation.uniqueId = 2

    map.addAnnotation(annotation2)

    //Showing the device location on the map
    self.map.showsUserLocation = true;

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    var view = mapView.dequeueReusableAnnotationView(withIdentifier: "AnnotationView Id")
    if view == nil{
        view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "AnnotationView Id")
        view!.canShowCallout = true
    } else {
        view!.annotation = annotation
    }

    view?.leftCalloutAccessoryView = nil
    view?.rightCalloutAccessoryView = UIButton(type: UIButtonType.detailDisclosure)


    return view
}

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

    if (control as? UIButton)?.buttonType ==   UIButtonType.detailDisclosure {
        mapView.deselectAnnotation(view.annotation, animated: false)
        if let myAnnotation = view.annotation as? MyAnnotation {
            if (myAnnotation.uniqueId == 1) {
                performSegue(withIdentifier: "info", sender: view)
            }
            else  {
                performSegue(withIdentifier: "info2", sender: view)
            }
        }            
    }
}

}

Solution

  • The simplest way to know on which annotation you tap is using creating custom annotation class and adding annotation of it. So create one annotation class MyAnnotation child class of MKPointAnnotation and maintain one uniqueId with your multiple annotation.

    class MyAnnotation: MKPointAnnotation {
        var uniqueId: Int!
    }
    

    Now you need to add annotation of type MyAnnotation instead of MKPointAnnotation.

    let annotation = MyAnnotation()
    annotation.coordinate = locations
    annotation.title = "Zaid Homes"
    annotation.subtitle = "Hay aljameaa"
    //Set uniqueId for annotation
    annotation.uniqueId = 1    
    map.addAnnotation(annotation)
    
    let annotation2 = MyAnnotation()
    annotation2.coordinate = location2
    annotation2.title = "Zaid "
    annotation2.subtitle = "aljameaa"
    //Set uniqueId for annotation
    annotation2.uniqueId = 2    
    map.addAnnotation(annotation2)
    

    Now check this uniqueId in calloutAccessoryControlTapped method on which annotation you tapped.

    func mapView(_ mapView: MKMapView, annotationView view:  MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
    
        if (control as? UIButton)?.buttonType ==   UIButtonType.detailDisclosure {
            mapView.deselectAnnotation(view.annotation, animated: false)
            if let myAnnotation = view.annotation as? MyAnnotation {
                if (myAnnotation.uniqueId == 1) {
                     performSegue(withIdentifier: "info1", sender: view)
                }
                else  {
                     performSegue(withIdentifier: "info2", sender: view)
                }
            }            
        }
    }