iphoneios6storyboardmapkitcallouts

show left and right callout accessory in map view using map kit


i have a simple problem that i cant figure out the answer to. i am extracting the long and lat of locations from within the plist and placing them on the map view. this part works just fine. pins show and the title and the subtitle (again from the plist) show as well. here is the code that for my view controller .h class:

#import "ViewController.h"
#import "Annotation.h"

@interface ViewController ()

@end


#define WIMB_LATITUDE 51.434783;
#define WIMB_LONGITUDE -0.213428;


#define ARSENAL_LATITUDE 51.556899;
#define ARSENAL_LONGITUDE -0.106403;

#define CHELSEA_LATITUDE 51.481314;
#define CHELSEA_LONGITUDE -0.190129;


#define THE_SPAN 0.10f;

@implementation ViewController
@synthesize myMapView;

- (void)viewDidLoad
{
[super viewDidLoad];

MKCoordinateRegion myRegion;
CLLocationCoordinate2D center;
center.latitude = ARSENAL_LATITUDE;
center.longitude = ARSENAL_LONGITUDE;
MKCoordinateSpan span;
span.latitudeDelta = THE_SPAN;
span.longitudeDelta = THE_SPAN;
myRegion.center = center;
myRegion.span=span;
[myMapView setRegion:myRegion animated:YES];

NSMutableArray *annotations = [[NSMutableArray alloc]init];
NSString *path = [[NSBundle mainBundle] pathForResource:@"Stadiums" ofType:@"plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
NSArray *anns = [dict objectForKey:@"Stadiums"];
NSLog(@"read1");

for(int i = 0; i < [anns count]; i++) {
    NSLog(@"read2");
    float realLatitude = [[[anns objectAtIndex:i] objectForKey:@"Latitude"] floatValue];
    float realLongitude = [[[anns objectAtIndex:i] objectForKey:@"Longitude"] floatValue];
    NSLog(@"read3");

    Annotation *myAnnotation = [[Annotation alloc] init];
    CLLocationCoordinate2D theCoordinate;
    theCoordinate.latitude = realLatitude;
    theCoordinate.longitude = realLongitude;
    myAnnotation.coordinate = theCoordinate;
    myAnnotation.title = [[anns objectAtIndex:i] objectForKey:@"Title"];
    myAnnotation.subtitle = [[anns objectAtIndex:i] objectForKey:@"Subtitle"];
    [myMapView addAnnotation:myAnnotation];
    [annotations addObject:myAnnotation];
    //[myAnnotation release];
}

}

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

MKAnnotationView *annotationView = (MKAnnotationView *)[myMapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];

if (annotationView == nil)
{
    annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
}

UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside];
annotationView.rightCalloutAccessoryView = rightButton;

UIImageView *IconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"toilets.png"]];
annotationView.leftCalloutAccessoryView = IconView;

annotationView.canShowCallout = YES;
annotationView.annotation = annotation;
return annotationView;
}


- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

my issue is that the left and right callout accessories never gets called. when pressing on the pin i just get the title and the subtitle. i have tried so many different ways but cant seem to solve this issue. can any one please help me repair this code so i can get the accessories to show.

thanks a million

p.s. i am using iOS 6 and storyboard and the project is based on iphone only.


Solution

  • ah, got it. as Anna was right in her hint about the delegate not being set. i was setting the delegate in the wrong way. all i had to do was add the to my view controller header view rather than trying to input it in my annotation view and then call the delegate in view controller's view did load method to self, like this: myMapView.delegate = self; the code was working all along, but because of the missing delegate my view for annotation was not getting fired. once again thanks to Anna Karennina for the hint.