I would like to work with an annotation once it's clicked. I've looked it up on the documentation of Apple and I did googled but I can not find why this is any different than how it should be done.
Basically; I don't get a println("Pin clicked");
Why not?
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView!
{
if !(annotation is MKPointAnnotation) {
return nil
}
let reuseId = "test"
var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
if anView == nil {
anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
anView.canShowCallout = true
}
else {
anView.annotation = annotation
}
return anView
}
func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!)
{
println("Pin clicked");
}
func setAnnotationPinOnMap(annotation : NSManagedObject)
{
var subject: AnyObject? = annotation.valueForKey("subject")
var desc: AnyObject? = annotation.valueForKey("desc")
var langitude: AnyObject? = annotation.valueForKey("langitude")
var longitude: AnyObject? = annotation.valueForKey("longitude")
println(annotation);
let pin = MKPointAnnotation()
let location = CLLocationCoordinate2D(
latitude: longitude as CLLocationDegrees,
longitude: langitude as CLLocationDegrees
)
println(location.longitude, location.latitude)
pin.setCoordinate(location)
pin.title = subject as String!
pin.subtitle = desc as String!
println( pin.coordinate.longitude)
viewForAnnotation(pin);
mapView.addAnnotation(pin)
}
I have imported map kit and included the map view delegate.
delegate
property set? If not, the delegate method won't get called. Also make sure the annotation'stitle
(which is set tosubject
) is not blank or nil. – Gemmalocation
, latitude is set to longitude and longitude is set to langitude which seems backwards. 2) The viewForAnnotation function as written is pointless. It returns a view but the code does nothing with it and it's not named correctly as the map view's delegate method. It must be namedfunc mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView!
and the map view'sdelegate
must be set. You're not supposed to call it directly. – Gemma