Peter Gaffney Peter Gaffney - 4 months ago 14
Swift Question

Add extra detail to MKPointAnnotation other than title and subtitle

I wish to pass informations from a Pin annotation to another viewController.I can pass the title and subtitle of the annotation but I need to pass some extra information along with these. Is there a way to add extra information to a MKPointAnnotation other than just title and subtitle?

here I have the pin title and subtitle set so it appears on the map:

var zoopin = MKPointAnnotation()
zoopin.coordinate = zoo
zoopin.title = "The zoo"
zoopin.subtitle = "hello this is the zoo"
mapView.addAnnotation(zoopin)


the title and subtitle are then passed to my info view controller using:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "info") {
if let annotation = sender as? MKAnnotationView {
let detailViewController = segue.destinationViewController as! info
detailViewController.titleText = annotation.annotation?.title ?? ""
detailViewController.detaileText = annotation.annotation?.subtitle ?? ""

}
}
}

Lu_ Lu_
Answer

make your own annotation, new file or class

import MapKit

class MyAnnotation: NSObject, MKAnnotation {
    var coordinate: CLLocationCoordinate2D
    var EXTRA_INFORMATION: String?
    var title: String?

    init(coordinate: CLLocationCoordinate2D) {
        self.coordinate = coordinate
    }
}

and use it instead of normal MKPointAnnotation

var zoopin = MyAnnotation()
zoopin.coordinate = zoo
zoopin.title = "The zoo"
zoopin.subtitle = "hello this is the zoo"
zoopin.EXTRA_INFORMATION = "that is your new extra info that you wanted to add?"
mapView.addAnnotation(zoopin)