user605957
user605957

Reputation: 2489

selectAnnotation does not show callout

I want to change the callout shown on the MKMapView. When I call

[self.mapView selectAnnotation:annotation animated:YES]; 

it pans the map to the annotation, but does not show its callout. How do I get it to show the callout?

Upvotes: 2

Views: 1598

Answers (2)

Jing
Jing

Reputation: 1965

You should implement the MKAnnotation Delegate method

-(MKAnnotationView *)mapView:(MKMapView *)_mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    static NSString *AnnotationViewIdentifier = @"AnnotationViewIdentifier";

    MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[_mapView  dequeueReusableAnnotationViewWithIdentifier:AnnotationViewIdentifier];
    if (annotationView == nil) 
    {
       annotationView = [[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:AnnotationViewIdentifier]autorelease];
    }

    annotationView.canShowCallout = YES;

    return annotationView;
}

Upvotes: 0

Anurag
Anurag

Reputation: 141879

Set the title, and optionally the subtitle property on the annotation object. MapKit will automatically show a callout.

@interface MyAnnotation : NSObject <MKAnnotation> {
    NSString *title;
    NSString *subtitle;
}

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

@end

And the implementation,

#import "MyAnnotation.h"

@implementation MyAnnotation

@synthesize title, subtitle;

@end

Usage,

MyAnnotation *foo = [[MyAnnotation alloc] init];
foo.title = @"I am Foo";
foo.subtitle = "I am jus' a subtitle";

Upvotes: 2

Related Questions