2

I have a mapView populated with markers using MKAnnotations. I am able to get the array of annotations fine. However, how do I figure out the index of the marker that is tapped? Say I tapped a marker and the MKAnnotation popped up. How do I get this instance of the annotation?

ViewDidAppear code:

    for (var i=0; i<latArray.count; i++) {

    let individualAnnotation = Annotations(title: addressArray[i],
        coordinate: CLLocationCoordinate2D(latitude: latArray[i], longitude: longArray[i]))

    mapView.addAnnotation(individualAnnotation)
    }        
    //store annotations into a variable
    var annotationArray = self.mapView.annotations

    //prints out an current annotations in array
    //Result:  [<AppName.Annotations: 0x185535a0>, <AppName.Annotations: 0x1663a5c0>, <AppName.Annotations: 0x18575fa0>, <AppName.Annotations: 0x185533a0>, <AppName.Annotations: 0x18553800>]
    println(annotationArray)

Annotations class: import MapKit

class Annotations: NSObject, MKAnnotation {
    let title: String
    //let locationName: String
    //let discipline: String
    let coordinate: CLLocationCoordinate2D

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

    super.init()
}

var subtitle: String {
    return title
}
}
Josh O'Connor
  • 4,694
  • 7
  • 54
  • 98

1 Answers1

1

MKMapViewDelegate provides a delegate method mapView:annotationView:calloutAccessoryControlTapped: Implementing this method provides you with the MKAnnotationView of the MKAnnotation instance you're looking for. You can call the MKAnnotationView's annotation property to get the corresponding instance of the MKAnnotation.

import UIKit
import MapKit

class ViewController: UIViewController, MKMapViewDelegate {

   @IBOutlet weak var mapView: MKMapView!

   var sortedAnnotationArray: [MKAnnotation] = [] //create your array of annotations.

   //your ViewDidAppear now looks like:
   override func viewDidAppear(animated: Bool) {
       super.viewDidAppear(animated)
       for (var i = 0; i < latArray.count; i++) {
         let individualAnnotation = Annotations(title: addressArray[i], coordinate: CLLocationCoordinate2D(latitude: latArray[i], longitude: longArray[i]))
         mapView.addAnnotation(individualAnnotation) 
         //append the newly added annotation to the array
         sortedAnnotationArray.append(individualAnnotation)
       } 
    }     



  func mapView(mapView: MKMapView!, annotationView view:    MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {
       let theAnnotation = view.annotation
        for (index, value) in enumerate(sortedAnnotationArray) {
            if value === theAnnotation {
                println("The annotation's array index is \(index)")
            }
        }
    }
}
Judson Douglas
  • 331
  • 1
  • 5
  • Hi Judson, this method works, however it is not organized by when the marker was created. It seems to randomly pull the markers into an array. Do you know how to organize the index by when it was created? – Josh O'Connor Jul 19 '15 at 20:21
  • 1
    the mapView.annotations array isn't sorted by the time the annotation was added to the map view. You could keep your own sorted array of annotations. So you start with an empty array, and every time you call mapView.addAnnotation(), also add that annotation to the end of your array. The result will be an array of annotations sorted from earliest to latest. Then, instead of looping through mapView.annotations, like above, loop through the sorted array of annotations you've created. – Judson Douglas Jul 19 '15 at 20:54