So the default red annotations are showing in the Simulator absolutely fine, but the custom annotation MyPin wont show. I have a feeling this is something to do with the NSMutableArray? If so I'm not too sure of a way round it?
Hopefully there a Guru on the forum that can shed some light (hopefully its not a really rookie error, i have trawled through this for the past 2 hours and i cant see anything.)
#import "UKFlightsEuropeMap.h"
#import "Annotation.h"
@interface UKFlightsEuropeMap ()
@end
@implementation UKFlightsEuropeMap
@synthesize europeMapView;
//Define the Long and Lat.
#define EUROPE_LATITUDE 47.3690;
#define EUROPE_LONGITUDE 8.5380;
#define LONDON_LATITUDE 51.5171;
#define LONDON_LONGITUDE 0.1062;
#define PARIS_LATITUDE 48.8742;
#define PARIS_LONGITUDE 2.3470;
#define BRUSSELS_LATITUDE 50.75;
#define BRUSSELS_LONGITUDE 4.53333;
#define BERLIN_LATITUDE 52.5;
#define BERLIN_LONGITUDE 13.35;
#define THE_SPAN 0.01f;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[europeMapView setDelegate:self];
// Do any additional setup after loading the view.
//CREATE MY REGION
MKCoordinateRegion myRegion;
//SET CENTRE
CLLocationCoordinate2D center;
center.latitude = EUROPE_LATITUDE;
center.longitude = EUROPE_LONGITUDE;
//SET SPAN (ZOOM)
MKCoordinateSpan span;
span.latitudeDelta = THE_SPAN;
span.longitudeDelta = THE_SPAN;
myRegion.center = center;
myRegion.span = span;
//Set our europeMapView
[europeMapView setRegion:myRegion animated:YES];
//Annotation
NSMutableArray * locations = [[NSMutableArray alloc] init];
CLLocationCoordinate2D location;
Annotation * myAnn;
//London Annotation
myAnn = [[Annotation alloc] init];
location.latitude = LONDON_LATITUDE;
location.longitude = LONDON_LONGITUDE;
myAnn.coordinate = location;
myAnn.title = @"London";
myAnn.subtitle = @"London City";
[locations addObject:myAnn];
//Paris Annotation
myAnn = [[Annotation alloc] init];
location.latitude = PARIS_LATITUDE;
location.longitude = PARIS_LONGITUDE;
myAnn.coordinate = location;
myAnn.title = @"Paris";
myAnn.subtitle = @"City of Love";
[locations addObject:myAnn];
//Brussels Annotation
myAnn = [[Annotation alloc] init];
location.latitude = BRUSSELS_LATITUDE;
location.longitude = BRUSSELS_LONGITUDE;
myAnn.coordinate = location;
myAnn.title = @"Brussels";
myAnn.subtitle = @"Brussels";
[locations addObject:myAnn];
//Berlin Annotation
myAnn = [[Annotation alloc] init];
location.latitude = BERLIN_LATITUDE;
location.longitude = BERLIN_LONGITUDE;
myAnn.coordinate = location;
myAnn.title = @"Berlin";
myAnn.subtitle = @"Berlin";
[locations addObject:myAnn];
[self.europeMapView addAnnotations:locations];
}
//CUSTOMISE THE ANNOTATION CALLOUTS.
-(MKAnnotationView *) europeMapView:(MKMapView *)europeMapView viewForAnnotation: (id<MKAnnotation>)annotation { MKPinAnnotationView *MyPin=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"current"];
MyPin.pinColor = MKPinAnnotationColorPurple;
UIButton *advertButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[advertButton addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
MyPin.rightCalloutAccessoryView = advertButton;
MyPin.draggable = NO;
MyPin.highlighted = YES;
MyPin.animatesDrop = TRUE;
MyPin.canShowCallout = YES;
return MyPin;
}
-(void)button:(id)sender {
NSLog(@"Button Pressed");
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Thanks very much in advance.
Your viewforAnnotation
is not being called because you have changed the name
It should be
– mapView:viewForAnnotation:
but yours is called
- europeMapView:viewForAnnotation:
If you change it to this you'll have more success
-(MKAnnotationView *) mapView:(MKMapView *)aMapView viewForAnnotation:(id<MKAnnotation>)annotation
{
MKPinAnnotationView *MyPin=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"current"];
MyPin.pinColor = MKPinAnnotationColorPurple;
UIButton *advertButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[advertButton addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
MyPin.rightCalloutAccessoryView = advertButton;
MyPin.draggable = NO;
MyPin.highlighted = YES;
MyPin.animatesDrop = TRUE;
MyPin.canShowCallout = YES;
return MyPin;
}
Also, you can drop the addTarget
and implement – mapView:annotationView:calloutAccessoryControlTapped: which gives you the annotationView that was tapped, from that you can get the annotation that was shown at the time and thus determine what to sort of advert to show.