Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

didSelectAnnotationView not called

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.

like image 938
KnijnOps Avatar asked Nov 03 '14 11:11

KnijnOps


2 Answers

In the latest version of Swift, the method declaration has changed from this:

func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!)

to this:

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) 
like image 173
CamQuest Avatar answered Oct 16 '22 17:10

CamQuest


In my case the problem was that i was setting the property canShowCallout in YES, so if you set this property in YES

-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view

isn't called. when i removed this property the method was called.

annotationView = [[ClusterAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
annotationView.canShowCallout = YES; //try setting to NO or don't set.
like image 44
GOrozco58 Avatar answered Oct 16 '22 19:10

GOrozco58