mike vorisis
mike vorisis

Reputation: 2832

How can I detect which annotation was selected in MapView

I've made a few annotations inside the Map and when I tap on them I see some informations and I have a button that opens Maps and with the right information that I can't take it should draw me my route.

Here is my code:

I have 2 arrays of double for my lat & lon That I filled them from my query.

 var lat = [Double]()
 var lon = [Double]()

These lines are for filling annotations

self.annot = Islands(title: object["name"] as! String, subtitle: object["address"] as! String,  coordinate: CLLocationCoordinate2D(latitude: (position?.latitude)!, longitude: (position?.longitude)!), info: object["address"] as! String)

self.map.addAnnotations([self.annot])

This is where the annotation is creating:

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {

    let identifier = "pin"

    if annotation.isKindOfClass(MKUserLocation) {
        return nil
    }

    let image = UIImage(named: "car")
    let button = UIButton(type: .Custom)
    button.frame = CGRectMake(0, 0, 30, 30)
    button.setImage(image, forState: .Normal)
    button.addTarget(self, action: #selector(Map.info(_:)), forControlEvents:UIControlEvents.TouchUpInside)

    var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)
    if  annotationView == nil {
        annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "pin")
        annotationView!.canShowCallout = true
        annotationView!.image = UIImage(named: "annotation")
        annotationView!.rightCalloutAccessoryView = button

    }
    else {
        annotationView!.annotation = annotation
    }

    return annotationView

}

And finally this is the button's function where I should pass the right info ( and where the problem is)

   func info(sender: UIButton)
    {

        let latitute:CLLocationDegrees  =  lat[sender.tag]
        let longitute:CLLocationDegrees =  lon[sender.tag]

        let regionDistance:CLLocationDistance = 10000
        let coordinates = CLLocationCoordinate2DMake(latitute, longitute)
        let regionSpan = MKCoordinateRegionMakeWithDistance(coordinates, regionDistance, regionDistance)
        let options = [
            MKLaunchOptionsMapCenterKey: NSValue(MKCoordinate: regionSpan.center),
            MKLaunchOptionsMapSpanKey: NSValue(MKCoordinateSpan: regionSpan.span)
        ]
        let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
        let mapItem = MKMapItem(placemark: placemark)
        mapItem.name = name[sender.tag]
        print(sender.tag)
        mapItem.openInMapsWithLaunchOptions(options)
    }

As you can see I tried with sender tag but with no luck.

EDIT: My Custom Annotation class

class Islands: NSObject, MKAnnotation {

    var title: String?
    var addr: String?
    var coordinate: CLLocationCoordinate2D
    var info: String?
    var subtitle: String?

    init(title: String, subtitle: String, coordinate: CLLocationCoordinate2D, info: String) {
        self.title = title 
        self.coordinate = coordinate
        self.info = info
        self.subtitle = subtitle

    }

}

Upvotes: 12

Views: 13587

Answers (2)

Rajesh Maurya
Rajesh Maurya

Reputation: 3154

For Swift 4

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    var selectedAnnotation = view.annotation
}

Upvotes: 7

Nirav D
Nirav D

Reputation: 72410

For that you can use selected Annotation from didSelectAnnotationView, then store that annotation to instance variable and after that used annotation in your Button action method.

var selectedAnnotation: MKPointAnnotation?

func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
    self.selectedAnnotation = view.annotation as? MKPointAnnotation
}

func info(sender: UIButton) {
    print(selectedAnnotation?.coordinate)
}

Edit: As of you have custom MKAnnotation class you need to use that.

var selectedAnnotation: Islands?

func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
    self.selectedAnnotation = view.annotation as? Islands
}    

Upvotes: 16

Related Questions