所以我想在我的 mkmap 视图中添加一个自定义注释。我已将其追溯到我的自定义注释类中的一个问题(见下文)。有人看到我的代码有问题吗?
//// .h 文件 /////
@interface AddressAnnotation : MKAnnotationView
@property (nonatomic, strong) NSString *address;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, strong) UIImage *image;
-(id)initWithCoordinate:(CLLocationCoordinate2D)coord;
-(id)initWithCoordinate:(CLLocationCoordinate2D)coord andAddress:(NSString *)address;
@end
/// .m ///////////
@implementation AddressAnnotation
@synthesize image = _image;
-(id)initWithCoordinate:(CLLocationCoordinate2D)coord {
return [self initWithCoordinate:coord andAddress:nil];
}
-(id)initWithCoordinate:(CLLocationCoordinate2D)coord andAddress:(NSString *)address {
if (self = [super init]) {
self.coordinate = coord;
self.address = address;
}
return self;
}
@end
我还应该说明我在我的 .h 中尝试过这个,但它没有帮助。
@interface AddressAnnotation : MKAnnotationView <MKAnnotation>
我也知道它的注释中的代码,因为我将注释的类型更改为我知道有效的类型,并将其放置在地图上。
=== 更新 ================ 根据您的要求提供更多代码。
下面是我的 viewForAnnotation 代码的两个示例。顶部是不工作的,底部是工作的。
/// 不起作用/////
else if([annotation isKindOfClass:[AddressAnnotation class]]){
static NSString *identifier = @"addressLocation";
AddressAnnotation *addAnno = (AddressAnnotation *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (!addAnno) {
addAnno = [[AddressAnnotation alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
}
addAnno.image = [UIImage imageNamed:@"Map_pin.png"];
addAnno.bounds = CGRectMake(0, 0, 32, 39); //initial bounds (default)
return addAnno;
/// 确实有效/////
else if([annotation isKindOfClass:[AddressAnnotation class]]){
static NSString *identifier = @"addressLocation";
HGMovingAnnotationView *addAnno = (HGMovingAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (!addAnno) {
addAnno = [[HGMovingAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
}
addAnno.image = [UIImage imageNamed:@"Map_pin.png"];
addAnno.bounds = CGRectMake(0, 0, 32, 39); //initial bounds (default)
return addAnno;
}