MKAnnotation not showing callout on MKMapView

Amit Shah picture Amit Shah · Nov 20, 2011 · Viewed 11.1k times · Source

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.

Answer

user467105 picture user467105 · Nov 21, 2011

The callout doesn't show because the title is nil.

It is not necessary to implement viewForAnnotation to show callouts since the default map view implementation shows callouts. (However, if you do implement it, you must set canShowCallout in that delegate method and not where you are doing it right now.)

Even if you set canShowCallout to YES, the callout still won't show if the title is nil or blank.

Log the tmp dictionary. Either the name key is blank or it doesn't exist.