Amit Shah
Amit Shah

Reputation: 4164

MKAnnotation not showing callout on MKMapView

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.

Upvotes: 15

Views: 11597

Answers (2)

user467105
user467105

Reputation:

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.

Upvotes: 45

Henri Normak
Henri Normak

Reputation: 4725

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.

Upvotes: 9

Related Questions