1

所以默认的红色注释在模拟器中显示得很好,但自定义注释 MyPin 不会显示。我有一种感觉,这与 NSMutableArray 有关吗?如果是这样,我不太确定如何解决它?

希望论坛上有一位大师可以提供一些启示(希望这不是一个真正的菜鸟错误,我在过去的 2 个小时里一直在浏览这个问题,但我什么也看不到。)

#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

首先十分感谢。

4

1 回答 1

1

viewforAnnotation没有被叫到,因为你改了名字

它应该是 – mapView:viewForAnnotation: ,但你的被称为 - europeMapView:viewForAnnotation:

如果您将其更改为此,您将获得更大的成功

-(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;

}

此外,您可以删除addTarget并实现- mapView:annotationView:calloutAccessoryControlTapped:它为您提供被点击的 annotationView,您可以从中获取当时显示的注释,从而确定要显示的广告类型。

于 2013-02-25T20:00:47.470 回答