0

所以我想在我的 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;
}
4

2 回答 2

0

在示例中创建另一个类 AuxLocation。

辅助位置.h

 #import < Foundation/Foundation.h>

 #import < mapKit/MKAnnotation.h>

@interface AuxLocation : NSObject
{

    CLLocationCoordinate2D coordinate;
    NSString *title;
    NSString *subtitle;
}

@property(nonatomic,assign)CLLocationCoordinate2D coordinate;

@property(nonatomic,copy) NSString *title;

@property(nonatomic,copy) NSString *subtitle;

@end

然后是 AuxLocation.m

**#import "AuxLocation.h"**

@implementation AuxLocation

@synthesize coordinate,title,subtitle;

@end

然后在要显示注释的类中

我的类.h

@interface MyClass : UIViewController
{

    AuxLocation *annotation;   


}

我的班级.m

- (void)viewDidLoad
{

    [super viewDidLoad];

    // Do any additional setup after loading the view.
    [super viewDidLoad];

    annotation = [[AuxLocation alloc] init];

    annotation.title=@"Antonella Pino Diseño Interior";

    annotation.coordinate=region.center;

    [mapview addAnnotation:annotation];   

}

希望这个结果有用。

于 2013-06-24T18:06:14.043 回答
0

不知道问题是什么,所以我只是将 AddressAnnotation 设置为一个有效的子类。下面的课。

@interface HGMovingAnnotationView : MKAnnotationView

@interface AddressAnnotation : HGMovingAnnotationView 
于 2013-06-24T15:51:10.930 回答