0

I have an object that implements the MKAnnotation protocol:

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface VoiceMemoryAnnotation : NSObject <MKAnnotation> {
    NSString * blobkey;
}
@property (nonatomic, retain) NSString * blobkey;

-(id)initWithBlobkey:(NSString *) key andCoordinate:(CLLocationCoordinate2D) c;
@end

Adding this object a map works perfectly since I can see the red pins being dropped. However, the problem arises when I want to set this object to show a callout.

I cannot do annotation.showCallOut=YES because an "MkAnnotation" does not have this property, but a MkAnnotationView does. How do I get around this?

I tried to implement the map callback "viewForAnnotation" to check for "VoiceMemoryAnnotation" and I try to return a new "MkAnnotationView" and set it's callout = YES, but I start to get a segmentation fault when I do this.

Any ideas what I"m doing wrong?

user1068636
  • 1,871
  • 7
  • 33
  • 57

2 Answers2

2

First you need to create your annotation object (the one that implements the MKAnnotation protocol) and add it to your map using something like

VoiceMemoryAnnotation*VMA = [[VoiceMemoryAnnotation alloc] init];
VMA.title = @"Title String";
VMA.subtitle = @"Subtitle String";

[self.mapView addAnnotation:VMA];

That will automatically call the following method which you will need to implement:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
 {
     MKPinAnnotationView*singleAnnotationView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:nil];
     singleAnnotationView.canShowCallout = YES;

     return singleAnnotationView;
 }

In this implementation MKAnnotationView won't work, it needs to be MKPinAnnotationView.

JJ_
  • 251
  • 1
  • 6
  • 1
    This was exactly what I needed. Also, I found out the reason why I was getting a "seg fault" is because I failed to add a title to the callout after setting the property to YES. Once I added title selector it worked perfectly and shows the callout with title. – user1068636 Mar 11 '12 at 07:55
0

I'm not sure I completely understand your question, but I wonder, is MKMapViews's - (void)selectAnnotation:(id <MKAnnotation>)annotation animated:(BOOL)animated what you're looking for?

QED
  • 9,803
  • 7
  • 50
  • 87