0

I want to be able to store a custom value for each dropped pin using MKPointAnnotation. Specifically, I want to store some id with each pin and retrieve at calloutAccesoryControlTapped.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Dan A
  • 209
  • 1
  • 3
  • 12
  • Is this a question on how to persist data, or on how to do something with MapKit? What do you mean by "store a custom value"? – Daniel T. Dec 14 '15 at 01:31

2 Answers2

3

You'll have to subclass MKPointAnnotation with a property to store this custom value (I named it tag)

import UIKit
import MapKit
class CustomPointAnnotation: MKPointAnnotation {
    var tag: Int!
}

Creating pins:

let annotation = CustomPointAnnotation()
annotation.coordinate = CLLocationCoordinate2D(latitude: CLLocationDegrees(latitude), longitude: CLLocationDegrees(longitude))
            annotation.title = [insert name]
            annotation.tag = [insert tag]
            self.mapView.addAnnotation(annotation)

And in your mapView delegate's viewForAnnotation, after checking the dequeableAnnotation you do:

  if (annotation is CustomPointAnnotation) {
       pinView?.tag = (annotation as! CustomPointAnnotation).tag
  }
Marcos Griselli
  • 1,306
  • 13
  • 22
1

Minor edit to Marcos Griselli's answer. pinView needs to be cast to access the custom tag.

if (annotation is CustomPointAnnotation) {
    (pinView?.annotation as! CustomPointAnnotation).tag = (annotation as! CustomPointAnnotation).tag
}
Community
  • 1
  • 1
Gr8Khan
  • 11
  • 3