Alex Rivera | Logout

MKAnnotation not showing callout on MKMapView

Asked 2011-11-20T17:49:18.907
15

I've got a MKMapView and I'm adding annotations like this:

for (NSDictionary *tmp in response)
{
    NSDictionary *places = [tmp objectForKey:@"place"];
    NSDictionary *location = [places objectForKey:@"location"];
    NSLog(@"long: %@ Lat:%@",[location objectForKey:@"longitude"], [location objectForKey:@"latitude"]);

    float longitude = [[location objectForKey:@"longitude"] floatValue];
    float latitude = [[location objectForKey:@"latitude"] floatValue];


    CLLocationCoordinate2D locationco = {latitude,longitude};
    NSString *titleString = [tmp objectForKey:@"name"];

    Place *pin = [[Place alloc] init];
    pin.coordinate = locationco;
    pin.title = titleString;
    pin.subtitle = @"A Location";

    //NSArray *annots = [[NSArray alloc] initWithObjects:pin, nil];
    //[map addAnnotations:annots];
    [map addAnnotation:pin];
    [[map viewForAnnotation:pin] setCanShowCallout:YES];
}

The MKAnnotation's show up on the map fine, and I can select them, however no callout bubble appears. I know that they are being selected properly form this

-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
    [view setCanShowCallout:YES];
    NSLog(@"Title:%@",[view.annotation description]);
}

But that just prints out

Title:(null)

I'm using ARC, and I've got the properties set up in my Place object as such:

@property (nonatomic) CLLocationCoordinate2D coordinate;
@property (nonatomic,readwrite, copy) NSString *title;
@property (nonatomic,readwrite, copy) NSString *subtitle;

What am I doing wrong/missing? Thanks.

Edit
Report

1 Answer

9

You need to implement MKMapViewDelegate method mapView:viewForAnnotation:, which creates the view and returns it. In that method call [view setCanShowCallout:YES]; on the view you plan to return for the annotation.

Because the mapView:didSelectAnnotationView: is called AFTER the pin has been selected it won't have any effect on enabling/disabling the callout.

answered 2011-11-20T21:31:46.043

Your Answer