2

I did this MapKit-Tutorial

Everything works fine, but how can I add an extra attribut to my pin?

This is my class Car:

import Foundation
import MapKit

class Car: NSObject, MKAnnotation {

let title: String
let subtitle: String
let thirdAttribut: String
let coordinate: CLLocationCoordinate2D

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

    super.init()
}


var getTitle: String {
    return title
}

var getSubtitle: String {
    return subtitle
}

var getThirdAttribut: String {
    return thirdAttribut
}

I want click on an pin and work with this the "thirdAttribut". My Main-Class:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    mapView.delegate = self
    let car = Car(title: "Title", subtitle: "Subtitle", thirdAttribut: "ThirdAttribut", coordinate: CLLocationCoordinate2D(latitude: 50.906849, longitude: 7.524224) )
    mapView.addAnnotation(car)

    }


func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
  //some config stuff
}



    func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!) {
    print("Here Comes the Click")

    print(view.annotation.title)
    print(view.annotation.subtitle)
    //view.annotation.thirdAttribut doesn't existis
    //How get I my "thirdAttribut"-Attribut?

    }

The third-Attribut mustn't appear in my view. It just contains some data for logic-operations.

I hope u understand me, english isn't my mother tongue.

If u know other ways to code what I want then please tell me. :)

Thank u!

kuzdu
  • 7,124
  • 1
  • 51
  • 69

1 Answers1

2

An annotation view contains an annotation property. To get that third attribute, you need to cast MKAnnotation to the class you created that adheres to the MKAnnotation protocol, Car.

if let carAnnotation = view.annotation as? Car{
    print(carAnnotation.thirdAttrib);
}
Josh Hamet
  • 957
  • 8
  • 10